The following regex if statement doesn't seem to work. I want to make sure the user enters between 2-3 letters and then between 7 - 11 numbers. otherwise return the error message.
add_filter( 'gform_field_validation_1_44', 'tax_id_verif', 10, 4 );
function tax_id_verif ( $result, $value, $form, $field )
{$tax_regex = "/^[a-zA-Z]{2,3}\s?+[1-9]{7,12}$/g";
$tax_match = preg_match($tax_regex, $value, $match);
if ($result != $tax_match) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a correct tax ID';
}
return $result;
}
The g
modifier is not supported by PHP regex. Also, \s?+
matches 1 or 0 whitespaces, you can use a mere \s*
. Also, [1-9]
only matches digits from 1
to 9
, it won't match a 0
. If you need to match 7 to 11 digits, use {7,11}
and not {7,12}
.
Use
$tax_regex = "/^[a-zA-Z]{2,3}\s*[0-9]{7,11}$/";
If there should be no whitespace between letters and digits, remove \s*
.