I have 3 single line fields (not name fields) in a specific form (form id 136). In these fields, I want people to be unable to write commas or semicolons. If they write a name containing this kind of punctuation, I need the form to display an error, asking the user to fix the input.
Example of correct input: John Doe
Incorrect input: Doe, John
Other incorrect input: Doe, John; Doe, Jane
What modifications would be necessary to accomplish this?
add_filter( 'gform_field_validation_136', 'gf_validate_name', 10, 4 );
function gf_validate_name( $result, $value, $form, $field ) {
if ( $field->type != 'name' ) {
return $result;
}
GFCommon::log_debug( __METHOD__ . '(): Name values => ' . print_r( $value, true ) );
if ( $result['is_valid'] ) {
foreach ( $value as $input ) {
if ( ! empty ( $input ) && ! preg_match( '/^[\p{L} ]+$/u', $input ) ) {
$result['is_valid'] = false;
$result['message'] = 'Sorry, your name must not contain commas or semicolons';
}
}
}
return $result;
}
Hi Samuel. Thank you, it worked perfectly.
Another doubt though: is there a way to use this same filter for 3 specific fields in this form, and 3 more fields in another form?
I know I can specify add_filter to one specific field using something like “gform_field_validation_136_11”. But if I had two more fields, 16 and 18, and another form with 3 other fields? In such case, do I have to add a unique filter to each field (copying and pasting this code multiple times, changing only form and field ID), or is there a way to use one filter for multiple, specific fields?
I can’t just assign this behavior to all fields in form 136, because there are other text fields that could use commas and semicolons.
Hi Eduardo. There are several approaches to this. You can use the same function, with multiple add_filter lines, each filter line targeting a different form and field. You will need to change from an anonymous function to a named function, like this:
add_filter( 'gform_field_validation_136_11', 'prevent_commas', 10, 4 );
add_filter( 'gform_field_validation_136_16', 'prevent_commas', 10, 4 );
add_filter( 'gform_field_validation_136_18', 'prevent_commas', 10, 4 );
add_filter( 'gform_field_validation_119_14', 'prevent_commas', 10, 4 );
add_filter( 'gform_field_validation_119_10', 'prevent_commas', 10, 4 );
add_filter( 'gform_field_validation_119_17', 'prevent_commas', 10, 4 );
function prevent_commas( $result, $value, $form, $field ) {
GFCommon::log_debug( __METHOD__ . '(): Running...' );
// Only for Single Line Text
if ( $field->type == 'text' ) {
if ( $result['is_valid'] ) {
if ( strpos( $value, ',' ) !== false || strpos( $value, ';' ) !== false ) {
GFCommon::log_debug( __METHOD__ . '(): Not allowed character detected: ' . $value );
$result['is_valid'] = false;
$result['message'] = 'Sorry, you used a not allowed character.';
}
}
}
return $result;
}
Another approach, if this becomes too much to manage, would be to give the field where you want to prevent commas and semicolons a Custom CSS Class name and then use the original code to target fields with that class (rather than the field type). Let us know if this method I shared with multiple add_filter lines does not work for you.