-1

I'm adding a pattern to a CF7 field using JS. Using the following pattern, my form field works perfectly (the field is shown as invalid when it should be).

pattern="^[0-9a-zA-Z .,'-]+( [0-9a-zA-Z .,'-]+)+$"

However, using Contact Form 7, the form will still send.

Below I have used the same Regular Expression within the Functions file. However, even correct entries don't work - all form entries are shown as invalid.

add_filter( 'wpcf7_validate_text*', 'custom_text_validation_filter', 20, 2 );

function custom_text_validation_filter( $result, $tag ) {
    if ( 'FieldName' == $tag->name ) {
        
        $re = "^[0-9a-zA-Z .,'-]+( [0-9a-zA-Z .,'-]+)+$";

        if (!preg_match($re, $_POST['FieldName'], $matches)) {
            $result->invalidate($tag, "This is not valid." );
        }
    }

    return $result;
}

0 Answers0