Created
April 18, 2025 12:38
-
-
Save faisalahammad/d14d4eef2e35c64de8294876bb689189 to your computer and use it in GitHub Desktop.
Simple PHP code to restrict Gravity Forms fields to English letters, numbers, spaces, and basic punctuation only. Prevents foreign characters on form submission.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('gform_field_validation', function($result, $value, $form, $field) { | |
// Specify the field IDs you want to restrict (example: 1, 2) | |
$restricted_field_ids = [1, 2]; | |
if (in_array($field->id, $restricted_field_ids)) { | |
// If value is an array (like Name field), convert it to a string | |
if (is_array($value)) { | |
$value = implode(' ', $value); | |
} | |
// Regex to allow 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment