Jump to content

Validating a US Phone number - basic facts


Love2c0de

Recommended Posts

Good morning,

 

I need to validate a US phone number and wanted to know some basic facts such as max/max amount of digits possible etc.

 

I know it can be validated with a regexp but just looking to do some simple tests on the data before inserting to a db.

 

Even if you could link me to a proven, reliable online resource that would be great.

 

Thanks for your time and help.

 

Kind regards,

 

L2c.

 

 

Link to comment
Share on other sites

A US phone number has a 3 digit area code and a 7 digit local number. So, you would want to validate that the value contains 10 digits. There are many ways that the value can be formatted:

 

123.456.7890

(123) 456-7890

123-456-7890

etc.

 

Plus, some 3 digit combinations are not valid as area codes - but I'm not sure what the full list is. I would just strip out everything that is not a number and verify the result is 10 characters long.

Link to comment
Share on other sites

The full details of U.S. phone numbers are available at the North American Numbering Plan Administration website: http://www.nanpa.com/

 

The basis are as Psycho stated: 10 digits (although some people can still get away with dialing only seven digits to reach someone in their own area code) - 3-digit area code, 3-digit exchange, and 4-digit number.

 

The area code can't start with 0 (zero) or 1 (one); and certain combinations are reserved.

 

The 1- you often see at the begining of a 11-digit number is used to indicate a long-distance call (toll or toll-free). It is not part of the assigned number. All numbers in the NANP are 10 digits.

Link to comment
Share on other sites

I wouldn't bother with validating if it's a real number. Best case scenario is you validate against known reserved numbers and some current ranges, but it's an ever-changing list so you'll always have to keep that info up-to-date. Also, that doesn't account for people who could lie about their phone number with a perfectly valid number.

 

As Psycho mentioned, easiest thing to do is just strip out all non-numbers and count how many numbers there are. If you already know it's supposed to be a US phone number, then expect 10 digits. If it's among a list of international numbers, expect 11 digits. Usually someone can just use the last 7 digits when calling someone locally (like next door neighbor) but nobody in their right mind hands out their phone number without the area code (the full 10 digits) unless it is to their neighbor.

 

Don't bother accounting for extensions. An extension can be literally any number. If you want to receive someone's extension, make sure it's received in a separate field. If by some chance you are having to work with a list with them already in it, then there's no official format for it, but "usually" people separate the main number and the extension with some variation of "x" "ex" or "ext" (which may or may not have a dot at the end). Best I've come up with (which I've mostly never had issues with, but isn't guaranteed 100%) is before stripping out all non-numbers, I look for first letter char and strip it and anything else after that. This is because there is absolutely no convention for having letters in a US phone number except to someone note an extension. So overall, this is what I usually do:

 

$number = preg_replace('~[a-z].*$~i','',$number);
$number = preg_replace('~[^0-9]~','',$number);
if ( strlen($number)==10 ) {
  // valid US number, woo!
} else {
  // fuck this noise
}
If you really, really need to ensure it's a real working phone number and hopefully associated with the person giving it (e.g. they are signing up for a paid-for service and if they fail to pay, you need to contact them and threaten to break their legs), I would suggest using a paid 3rd-party service that will verify a number and return info to match against other info entered in (like the person's name, address, etc.)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.