Okay, so my code does work. But, I need to edit some of it. For example, I want it to allow numbers 7 or 10 numbers in length.
The second part is that it is not validating my numbers with -
or ()
in them, which it should. I want it to be able to validate numbers with the parenthesis or hyphens, no letters.
<?php
$phoneNumbers = array("111-1111",
"(111) 111-1111",
"111-111-1111",
"111111",
"",
"aaa-aaaa");
foreach ($phoneNumbers as $phone) {
$failures = 0;
echo "<hr /><p>Checking “$phone”</p><hr />";
// Check for 7 or 10 characters long
if (strlen($phone) < 7) {
++$failures;
echo "<p><small>Warning: “$phone” must be 7 or 10 characters</small></p>";
}
// Check for numbers
if (!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone)) {
++$failures;
echo "<p><small>Warning: “$phone” contains non numeric characters</small></p>";
}
if ($failures == 0)
echo "<p>“$phone” is valid</p>";
else
echo "<p>“$phone” is not valid</p>";
}
?>