Topic: Regular expressions help - phone number validation

I'm working on a form that needs to have the phone number validated for 10 digit numbers. Regular expressions is not something I seem to understand very easily. The area code is required, and may be enclosed by parentheses, or separated by the rest of the phone number by a space, hyphen or period.
The following phone numbers should validate:

123.456.7890
(123)456-7890
123-456-7890
123 456-7890
123 456 7890

The following phone numbers should not validate:
123.4567
123-4567
123 4567
abcdefghij

Below is the code I used to validate the phone number, but I'm not sure if I'm catching all possible matches/errors. Can someone who is a regular expressions expert/guru take a look at the code, and let me know what I might have missed?

if (!empty($phone)) {
               /* validate number */
              $checkPhone = '/(\d)?(\s|-|.)?(\()?(\d){3}(\))?(\s|-|.){1}(\d){3}(\s|-|.){1}(\d){4}/'; 
               /* reject phone number if it doesn't pass checkPhone */
               if (!preg_match($checkPhone, $phone)) {
                    array_push($missing, 'phone');
               }        
          }
"Change is inevitable, growth is intentional" - Glenda Cloud

Re: Regular expressions help - phone number validation

Why not just check for numbers only?  Let people type the number however they want, and just see if has 10 numbers in it, and remove whatever else.  you can format it how you want later.

I did a quick search around, and I found this and tested it out and made some changes.

function formatPhoneNumber($strPhone){
	$strPhone = ereg_replace("[^0-9]",'', $strPhone);
	if (strlen($strPhone) != 10){
			return false;
	}
	$strArea = substr($strPhone, 0, 3);
	$strPrefix = substr($strPhone, 3, 3);
	$strNumber = substr($strPhone, 6, 4);
	$strPhone = "(".$strArea.") ".$strPrefix."-".$strNumber;
	return ($strPhone);
}

$number = "555.666.0606";
$checkNumber =  formatPhoneNumber($number);
if(!$checkNumber){
	echo "Not valid!";
}else{
	echo $checkNumber." is valid!";
};

So basically whatever they input is valid as long as it contains 10 numbers total.  The number is then formatted however you want.  I can enter any of the following:
555.666.0606
555-666-0606
5556660606
(555)-666- 0606

and they all come out like this:
(555) 666-0606

Hope that helps!

Last edited by C.Barr (2008-03-30 20:08:20)

Re: Regular expressions help - phone number validation

Perhaps this might help, second code snippet down: http://www.symfony-project.org/snippets … er_by/date

Maybe not quite what you are looking for, but it does allow phones numbers as you'd expect them to be.

Re: Regular expressions help - phone number validation

Robert, Chris,

Thanks for checking this out, and providing different solutions! Both options look like they will work for me; I had not thought of selecting the area code, and other numbers separately. I'll test out the code later this afternoon.

"Change is inevitable, growth is intentional" - Glenda Cloud