Skip to content

Instantly share code, notes, and snippets.

@rickgregory
Last active April 29, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickgregory/ec2155f20b4becc662c017137b231edb to your computer and use it in GitHub Desktop.
Save rickgregory/ec2155f20b4becc662c017137b231edb to your computer and use it in GitHub Desktop.
//validation for name field in GF to prevent nonsense spam names. Rejects any string with 4 consecutive consonants.
add_filter( 'gform_field_validation', 'validate_name', 10, 4 );
function validate_name( $result, $value, $form, $field ) {
if ( 'name' === $field->type && $field->isRequired ) {
GFCommon::log_debug( __METHOD__ . '(): Name values => ' . print_r( $value, true ) );
$pattern = "/(?=[a-z]{4})[^aeiou]{4}/";
if ( !preg_match( $pattern, $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid name';
} else {
$result['is_valid'] = true;
$result['message'] = 'You may pass';
}
GFCommon::log_debug( __METHOD__ . '(): Result => ' . print_r( $result, true ) );
return $result;
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment