Need help to fix the regex in the preg_match statement on line 13.
This is for a comment field on the form and I want the comment to be able to be as long as needed, but they should only be able to use letters, numbers, exclamation point, question mark, commas, and periods, nothing else. In other words, a basic sentence or two.
Here's my code...
// Validate the message field is in regex format.
add_action( 'elementor_pro/forms/validation', function ( $record, $ajax_handler ) {
$fields = $record->get_field( [
'id' => 'message',
] );
if ( empty( $fields ) ) {
return;
}
$field = current( $fields );
if ( 1 !== preg_match( '/[^0-9a-z\s-]/i', $field['value'] ) ) {
$ajax_handler->add_error( $field['id'], 'Please write to us in English and no URLs allowed on this field.' );
}
}, 10, 2 );
Thanks in advance for your help.
Jaime
CodePudding user response:
Your regular expression matches something that contains a prohibited character. So you should use ===
, not !==
. Or just treat it as a boolean value without comparing to anything.
You need to add exclamation point, question mark, commas, and periods to the list of allowed characters in the regexp.
if (preg_match( '/[^!?,.0-9a-z\s-]/i', $field['value'] ) ) {