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

**URL:** <https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212>\
**Category:** Get Help\
**Tags:** gform\_field\_validation, name-field\
**Created:** [August 10, 2022, 9:36pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212 "2022-08-10T21:36:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmccrone](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/cmccrone/32/3049_2.png) [@cmccrone](https://community.gravityforms.com/u/cmccrone)\
**Post date:** [August 10, 2022, 9:36pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212/1 "2022-08-10T21:36:39Z")

</div>

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.

> [@Disable Special Characters but allow "Space"](https://community.gravityforms.com/t/disable-special-characters-but-allow-space/11290):
>
> So I want to disable special characters cause a 3rd party integration do not accept the special characters. I have found this code below, but it also doesn’t allow space. So how do I edit the code below, so it allows spaces but just not special characters like ÆÅØ and so on. 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; …

It denies Special Characters as it should

But now its not allowing me to submit the form

 ![](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/f7c6aaba29f46276c53ee2fb40565231e2a7f702.jpeg)  
Thoughts?

Thank you,  
Chris

---

<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:** [August 12, 2022, 3:48pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212/2 "2022-08-12T15:48:18Z")

</div>

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

Thank you.

---

<div class="post-metadata">

**Author:** ![cmccrone](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/cmccrone/32/3049_2.png) [@cmccrone](https://community.gravityforms.com/u/cmccrone)\
**Post date:** [August 17, 2022, 7:10pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212/3 "2022-08-17T19:10:42Z")

</div>

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

Here it is directly.

```auto
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](https://docs.gravityforms.com/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](https://docs.gravityforms.com/gform_field_validation/#h-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!

---

<div class="post-metadata">

**Author:** ![cmccrone](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/cmccrone/32/3049_2.png) [@cmccrone](https://community.gravityforms.com/u/cmccrone)\
**Post date:** [September 7, 2022, 10:03pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212/4 "2022-09-07T22:03:48Z")

</div>

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

---

<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:** [October 7, 2022, 10:04pm UTC](https://community.gravityforms.com/t/disallow-special-characters-in-name-field-or-using-a-css-class/13212/5 "2022-10-07T22:04:16Z")

</div>

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