How to disallow foreign characters in Gravity Forms and allow English characters only?

As some customers enter Chinese characters into the fields, such as Name, Message, I am looking for a type of functionality that disallows any foreign type of character sets other than English and all its special character variations to be typed into the input field. I want to allow only English data into our system.

Preferably a native feature already existing in Gravity Forms or another plugin that would add this feature. If this does not exist, I might go into the code to change it.

How do I achieve this please? Are there any easy solutions for this?

Hi there,

I’ve prepared a simple solution that checks the fields when the form is submitted and shows an error if any foreign characters are used. It works for normal text fields and also for complex fields like Name (which use arrays internally).

Here’s what you need to do:

  1. Add this PHP code to your theme’s functions.php file or use a plugin like “Code Snippets”:
add_filter('gform_field_validation', function($result, $value, $form, $field) {
    // Put your field IDs here that you want to restrict
    $restricted_field_ids = [1, 2];
    
    if (in_array($field->id, $restricted_field_ids)) {
        if (is_array($value)) {
            $value = implode(' ', $value);
        }
        
        // Allows English letters, numbers, spaces, and common punctuation
        if (!preg_match('/^[a-zA-Z0-9\s\.,\'"\-\!\?]*$/', $value)) {
            $result['is_valid'] = false;
            $result['message'] = 'Please enter English characters only.';
        }
    }
    return $result;
}, 10, 4);
  1. Replace [1, 2] with the IDs of the fields you want to restrict.

  2. Save and test your form.

This will prevent users from submitting characters outside the English alphabet and common punctuation.

Give it a try, and let me know how that goes! :grinning_face_with_smiling_eyes: