PHP code:
$phoneno = array(
"413 222 6859",
"4-(413)-222-6859",
"(413-222-6859",
"(413).222.6859",
"413 22 6859");
foreach ($phoneno as $number) {
echo "$number : ";
if (preg_match("/^
(1[-\s.])? # optional '1-', '1.' or '1'
( \( )? # optional opening parenthesis
\d{3} # the area code
(?(2) \) ) # if there was opening parenthesis, close it
[-\s.]? # followed by '-' or '.' or space
\d{3} # first 3 digits
[-\s.]? # followed by '-' or '.' or space
\d{4} # last 4 digits
$/x",$number)) { # x to ignore white spaces in pattern
echo "valid\n";
} else {
echo "invalid\n";
}
}
Output:
413 222 6859 : valid
4-(413)-222-6859 : valid
(413-222-6859 : invalid
(413).222.6859 : valid
413 22 6859 : invalid
No comments:
Post a Comment