Regex verification


I have been asked to add string baed verification to a form, which is pretty new to me so i am just looking for some advice.

The validation would be +20.00 to -20.00, the + or - would be optional.
I have the “regex” part for this.
I also have other fields to create using similar validations that use the +/- and some that dont, so i am guessing I can just use the same for them as well?

Is a “single line text” field the correct field to use?
Do i need to use this plugin??.. Search Results for “regex-textfield-gravityforms-ad” |

Or do you need to use the code from number 6. and then adapt or is there any other code out there to use?

If you have the regex, you can use the gform_field_validation filter. Example 6 is a good example showing a regex use with the filter. What have you tried so far?

I installed the plugin which gives me a regex field within gravity forms, it then just allows you to paste the regex in to the field options.

I guess i needed to use a single line of text field, then add your validation code.
If i take that number 6 as an example:

add_filter( 'gform_field_validation', 'validate_phone', 10, 4 );
function validate_phone( $result, $value, $form, $field ) {
    $pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)|\(?01\d{3}\)?)\s?\d{3}\s?\d{3}$/";
    if ( $field->type == 'phone' && $field->phoneFormat != 'standard' && ! preg_match( $pattern, $value ) && $value != '' ) {
        $result['is_valid'] = false;
        $result['message']  = 'Please enter a valid phone number';
    return $result;

I dont know what to change for:
1st line: Does validate_phone change? and the 10, 4? or do you just put validate_text ?
2nd line: Function, does this stay the same, apart from matching the “validate” part?
3rd line: I put my regex in there
4th line: Is this even needed? I guess it does but i dont know what needs to go in there.
5th line: Stays the same?
6th line: Message, i just put in what i want there.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.