# Regex verification

**URL:** <https://community.gravityforms.com/t/regex-verification/15544>\
**Category:** Get Help\
**Tags:** gform\_field\_validation, regex\
**Created:** [July 17, 2023, 5:34pm UTC](https://community.gravityforms.com/t/regex-verification/15544 "2023-07-17T17:34:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hairyhobo](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@hairyhobo](https://community.gravityforms.com/u/hairyhobo)\
**Post date:** [July 17, 2023, 5:34pm UTC](https://community.gravityforms.com/t/regex-verification/15544/1 "2023-07-17T17:34:25Z")

</div>

Hello

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” | WordPress.org](https://wordpress.org/plugins/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?

> **[gform\_field\_validation](https://docs.gravityforms.com/gform_field_validation/)**
>
> Use this filter to create custom validation logic for a field.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [July 18, 2023, 11:58pm UTC](https://community.gravityforms.com/t/regex-verification/15544/2 "2023-07-18T23:58:40Z")

</div>

If you have the regex, you can use the gform\_field\_validation filter. [Example 6](https://docs.gravityforms.com/gform_field_validation/#h-6-phone-field-validation) is a good example showing a regex use with the filter. What have you tried so far?

---

<div class="post-metadata">

**Author:** ![hairyhobo](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@hairyhobo](https://community.gravityforms.com/u/hairyhobo)\
**Post date:** [July 19, 2023, 10:20am UTC](https://community.gravityforms.com/t/regex-verification/15544/3 "2023-07-19T10:20:35Z")

</div>

> [@chrishajer](#):
>
> gform\_field\_validation filter

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [August 18, 2023, 10:20am UTC](https://community.gravityforms.com/t/regex-verification/15544/4 "2023-08-18T10:20:52Z")

</div>

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