Disable Special Characters but allow others special Characters

Hello I need to remove the special characters in address fields but not all. Specifically the characters “ñ”, “Ñ”, “º”, “ª” and “,” I need that if someone writes any special character other than those, a warning appears indicating that they are not valid characters and they change them before to submit the form.

I have seen the following code but I don’t know how to customize it to support those characters.

add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
$pattern = "/^[a-zA-Z ]*$/"; // note the space before the closing brace
if ( strpos( $field->cssClass, 'letters_spaces' ) !== false && ! preg_match( $pattern, $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Error: You are not allowed to use special characters';
return $result;
}, 10, 4 );

I have already solved to include those characters.
I just had to include the following [a-zA-ZªºñÑ,; ]

1 Like