Disallow special characters in name field (or using a CSS class)

Often times our customers autofill will put their email in the First Name field.

I found a solution to only allow letters and spaces and when I enable it works! It stops the special characters. But now it doesnt allow anything. I even tried all lower case text only and it gives me an error as if I entered special characters.

It denies Special Characters as it should

But now its not allowing me to submit the form


Thoughts?

Thank you,
Chris

Can you share the actual code you are using here, or use a code-sharing site to share the link?

Thank you.

No problem. I included it before. I linked to an article you had helped someone else with.

Here it is directly.

add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {

   if ( strpos( $field->cssClass, 'require_alpha_num' ) !== false && ! ctype_alnum( $value ) ) {

      $result['is_valid'] = false;
      $result['message'] = 'Error: You are not allowed to use special characters';

   }

   return $result;

}, 10, 4 );

I had reached out to GF support a little while back but it was too advanced for me. This is what they provided me with.

—————
Email from Gravity Forms
—————
Hello! You could hook to the filter gform_field_validation in order to check the value being submitted to that specific input on the name field. See the example 4. Name field validation for how you can investigate the various inputs of the name field as they are submitted. Checking for the existence of the @ character would seem to be sufficient in catching these.
———
END
———

Thanks for the help!

Im sure youre busy and thanks for the help!

Our CRM continues to get some emails in the first name field. I think its their autofill doing it.

Im just looking for simple way to prevent emails. It would be cool if the field allowed to say dont allow symbols or @ or anything like that.

Whatever you think is best.

Thanks,
Chris