Skip to main content
Topic: Validation functions? (Read 2819 times) previous topic - next topic

Validation functions?

Where have these been added?

I'm looking to Validate field input:
1) Telephone numbers, i.e. (###)###-#### in an input box
2) Currency - i.e. user enters 1200, is formatted an displayed as $1,200.00

Can someone explain how/where to do this?

Thanks,
Bill

Re: Number Formatting/Validation functions?

Reply #1
Here is the code provided in the gump.php file, distributed with PHPRad.  How do I access this for VALIDATION of an input field?

Surely there must be a way to use this function (and others inside gump.php)! :)

Code: [Select]
/**
     * Determine if the provided value is a valid phone number.
     *
     * Usage: '<index>' => 'phone_number'
     *
     * @param string $field
     * @param array  $input
     *
     * @return mixed
     *
     * Examples:
     *
     *  555-555-5555: valid
     *  5555425555: valid
     *  555 555 5555: valid
     *  1(519) 555-4444: valid
     *  1 (519) 555-4422: valid
     *  1-555-555-5555: valid
     *  1-(555)-555-5555: valid
     */
    protected function validate_phone_number($field, $input, $param = null)
    {
        if (!isset($input[$field]) || empty($input[$field])) {
            return;
        }

        $regex = '/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i';
        if (!preg_match($regex, $input[$field])) {
            return array(
              'field' => $field,
              'value' => $input[$field],
              'rule' => __FUNCTION__,
              'param' => $param,
            );
        }
    }

Re: Validation functions?

Reply #2
Bump - Anyone?

Re: Validation functions?

Reply #3
no one yet... i need to implement such validate function...  keep waiting...

Re: Validation functions?

Reply #4
Apologies for the late reply on this forum.

For validating custom telephone number, I would suggest something like regular expression.




For record formatting, you can use any of the PHP function for formatting the display of the field value.



Best Regards

Re: Validation functions?

Reply #5
function --to_number--  didn't work. It give result of function --number_to_words('en')-- instead.