Disable special characters in a text field input [RESOLVED]

I am wanting a text field that only allows numbers and letters. No special characters. I have been looking for a simple way to achieve this. Any ideas anyone?

Give something like this a try…

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'] = 'Please enter only numbers and letters';

   }

   return $result;

}, 10, 4 );

You’ll need to set the custom CSS class of require_alpha_num on the field.

Works perfectly. Thank you!

1 Like

I noticed as soon as I updated WordPress and theme, it removes the customization. Is there a way to do this that will not be removed with an update?

Yes. You will either want to set up a child theme or add the snippet via a custom plugin or some plugin that can run code snippets.

1 Like

Hey, thank you for the filter.

Can you please tell me, can i set this filter only for Form XY and Field XY, and the error show only if the customer write exact this two letters “HH” or “HJ”. It is possible?

Thank you, kind regards.