Regex validation small issue

Hi,

Can you guys help me figure out why the following regex isn’t allowing me to submit any email address whatsoever? No matter what I try it shows the error.

I want this to only work on Field id 12 on Form 3. This is a text field hence why I haven’t used $field ->type.

add_filter( ‘gform_field_validation’, ‘validate_email’, 10, 4 );
function validate_email( $result, $value, $form, $field ) {
pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)";
if ( $field->id == ‘12’ && ! preg_match( $pattern, $value ) ) {
$result[‘is_valid’] = false;
$result[‘message’] = ‘Please enter a valid email’;
}

return $result;

}

Please let me know if it is something wrong with my regex or is the code I have tried to adapt completely wrong?

Danny

Why are you attempting your own email address validation when that’s built in to Gravity Forms when using an actual Email field?

Hi Chris,

With the built in field we had tonnes of complaints from customers saying that the form kept telling them there email wasn’t valid even though it was.

Is there any documentation explaining what the form looks at when it’s verifying the standard email field as for us we just kept getting calls about it not working properly.

Danny

And you’re having the same issue with your custom regex? I think there is something else going on. The built in email field uses a built in PHP method to ensure the address is valid. They’ve spent way more time ensuring the email is valid than you or I ever could

I recommend opening a support ticket to figure out why the built email address validation is not working for you:

https://www.gravityforms.com/open-support-ticket/