'Regex in preg_match statement

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source