Love2c0de Posted December 12, 2013 Share Posted December 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/ Share on other sites More sharing options...
Psycho Posted December 12, 2013 Share Posted December 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462161 Share on other sites More sharing options...
requinix Posted December 12, 2013 Share Posted December 12, 2013 7 digits for a local number, 3+7 digits with the area code, '1'+3+7 digits (leading '1' + 10 digits) for a long-distance call. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462163 Share on other sites More sharing options...
DavidAM Posted December 12, 2013 Share Posted December 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462164 Share on other sites More sharing options...
cyberRobot Posted December 12, 2013 Share Posted December 12, 2013 Keep in mind that some people use extensions. Of course, that could be included in a separate field. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462185 Share on other sites More sharing options...
JIXO Posted December 12, 2013 Share Posted December 12, 2013 There is a better solution, libphonenumber is a PHP library based on Google's libphonenumber library, try it. Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462186 Share on other sites More sharing options...
.josh Posted December 14, 2013 Share Posted December 14, 2013 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.) Quote Link to comment https://forums.phpfreaks.com/topic/284726-validating-a-us-phone-number-basic-facts/#findComment-1462306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.