Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Phone Number

Status
Not open for further replies.

Meekos

Technical User
Feb 6, 2004
39
0
0
US
Can anyone tell me how to validate a phone number entered by our form users? I want to verify the number is in the format 999-999-9999 and digits not characters are used. For example, AAA-ZZZ-1111.

Help!

 
Most people will tell you to use ereg(), but I personally hate that function.

What I would is get rid of the dashes, run a is_int() check on the remaining string. And then, use substr() to check for specific characters in specific places.

Look up the functions at
 
If you don't want to use regular expression matching, use the following:
Code:
// assume the incoming phone number is in $_POST['phone']
$tmp = explode('-',$_POST['phone']);
if (count($tmp) != 3) $err_msg = 'Sorry phone number is in the wrong format';
for ($i=0;$i<3;$i++) 
    if (!is_numeric($tmp[$i]))
          $err_msg = 'Sorry phone number is in the wrong format';

To truely validate a phone number, you should compare the area code against all valid area codes, and the exchange against valid exchanges within an area code.

Of course this method only works in those countries where
the phone number is defined as "nnn-nnn-nnnn".

Ken
 
I think that simply checking that the pattern is o.k., and that it's integers is o.k., as if someone wants to input invalid data, they will just input 9999-9999-999 or something like that.

I personally am from Norway, and sometimes I visit those annoying sites where you need an valid areacode, etc.

How can I know which areacodes are valid?
I have no interest in learning the areacodes for a different country!

Why should they then block me as a customer, if I wish to contact them for some reason?

Keep that in mind!

Olav Alexander Mjelde
Admin & Webmaster
 
That's why I put in the disclaimer. If you're going to validate area codes and exchanges, you should only do so for calling areas that use them.

Telcordia ( (used to be Bellcore) still has the authority, I believe, in the United States to set area codes. Their web site should have more information.

Ken
 
Even if some people "hate" regular expressions, they are extremely helpful in data validation. I can understand some frustration with regex, but once you understand how they work and how to construct them, you'll start loving them.
To check the phone number pattern (including dashes):
Code:
$phone_number = '123-456-5559';
$pattern = "/\d{3}-\d{3}-\d{4}/";
if (!preg_match($pattern,$phone_number,$dummy)){
   echo("Please enter 999-999-9999.");
} else {
   # etc.
However, I also recommend to present the phone number in 3 parts in the UI. That will take care of the delimiter. Limit area code to max 3, exchange also 3, and number to 4. Assemble after submit.
If international numbers are needed, it gets a bit more complicated and no singular validation scheme will work (unless you just check for all numeric chars).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top