Bear with me, as I’m quite new to using regular expressions.
I have regexp checks on three fields, as marked in my code below. No matter what I enter in the fields, neither my character length restrictions, nor my actual character restrictions are enforced; all of these fields seem to simply “skip over” my regular expression checks and validate as true.
// NAME
if (preg_match('%^[A-Za-z\.\' \-]{12,80}$%', stripslashes($name)) === false) {
$name = false;
$errors[] = 'You need to enter a valid name. No slashes or quotes are allowed.';
}
else { echo 'Test: RegExp Validation passed for "NAME" <br />'; }
// PHONE NUMBER
if (preg_match('%^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$%', $phone) === false) {
$phone = false;
$errors[] = 'A valid phone number must be entered. Only numbers, dashes, and parentheses are allowed';
}
else { echo 'Test: RegExp Validation passed for "PHONE" <br />'; }
//EXTENSION
if (!empty($phnext) && preg_match('%^[0-9]{3,5}$%', $phnext) === false) {
$phnext = false;
$errors[] = 'Only 3 to 5 numbers are allowed for the phone extension.';
}
else { echo 'Test: RegExp Validation passed for "EXTENSION" <br />'; }
I don’t get any syntax, compilation, or out-of-range errors. So I’m wondering, are my actual regexp strings wrong, or is my logic wrong somewhere? It wouldn’t be the first time. I’m just a little stumped.
preg_match will only return
FALSEif an error occurred. In the case of no matches, it will return0. You should not be using a strict testpreg_match(...) === FALSE, instead you can use: