For a class I need to do a preg_match for a phone number. The number must match this format (###) ###-####. If the format is incorrect I need to send the user to the form to have the number filled out again. If a match is found I need to allow the form to go through.
My instructor had us write it as such:
$PhonePattern = "\(\d\d\d\) \d\d\d-\d\d\d\d";
if (!preg_match($PhonePattern, $Phone))
{
header('location:CreateAccount.htm');
exit();
}
else
{
$Phone = $_GET['txtPhone'];
}
This does not work. Can it be changed to:
if (!preg_match('/\(\d\d\d\) \d\d\d-\d\d\d\d/', $Phone))
{
header('location:CreateAccount.htm');
exit();
}
else
{
$Phone = $_GET['txtPhone'];
}
The following is one way:
I added anchors and repetition. Otherwise, you were close.
Learn more about regular expressions. They are a power tool. But with great power, comes great responsibility.