View Full Version : United States Telephones
boogyman
09-21-2007, 03:03 PM
Okay this really isn't anything more than me laffing at myself. I knew there would come a time when I would need to validate a US phone number. That time has come for a client of mine, so I set out on my quest to find a pre-made regular expression script for a usa phone number.
Many of the scripts I found fail very miserably and really are only set to test 1 or maybe 2 formats. So I decided I would just re-invent the wheel and create my own, but first I Need to see the different possibilities, and I was surprised to guess how many there are.
take your guess and in a week I will reveal the answer.
PS I know I should create one for international numbers too... but that is another option I am going to give the user
djr33
09-21-2007, 03:42 PM
A US phone number is standardized across the country, but can be presented in several formats. I believe it also is the same in Canada.
The main number consists of seven digits, in two groups. There is a general three number code that is consistent in a small area, let's say 234. That is then followed by four digits that rarely have any pattern, let's say 5678.
That number would then be commonly written as 234-5678.
That when dialed locally is all you need. For long distance calls, an area code is needed as well. These are large regions, like, near me, all of San Francisco has a single area code. (It's 415 if you're wondering.) For our example, we'll use 987.
The number is, in full, now, (987) 234-5678.
Note the format, parentheses around the area code, space, first group, hyphen, second group. That's the standard format I have always seen.
There are some other ways of writing this, though, but they are all the same number/format when dialing:
987-234-5678
9872345678
987.234.5678
987 234 5678
Those are all just different styles. I notice the dot separator frequently on the internet for a quick format.
Also, there is an implied 1 before the number which is used to signify that you are dialing a long distance number. That's where 1-800-numbers come from. 800 --- ---- is the same as 1 800 --- ----. Note that this is why I avoided number 1 as the start of the main number. (1 can exist in any position except the starting digit of the inner 7-digit number.)
So, a few more varieties:
1-987-234-5678
1 (987) 234-5678
1.987.234.5678
1 987 234 5678
And, I suppose, one could mix this any way they want.
The only exceptions I can think of:
1. Some numbers are represented as words. This is related to having letters on each number, abc on 2, def on 3, etc. (Q and Z are not represented, though sometimes attached to 1 or just added to the buttons they would be on if they fit.)
1-800-SOMETHING is an example.
Clearly, the word needs to have at least 7 characters.
But in many cases, it is longer. Dialing an extra number (or more) doesn't do anything, so that's just fine, really. That number really is 1-800-SOMETHI.
2. Cell phone gimmick numbers (not sure what this is really called) are now 5 digits (and/or represented by letters). These are unrelated to real phone numbers. (These are those weird things there are commercials for-- "text 123 to 12345 for a joke every day normaltextmessageratesapply99centspermessage".)
1. Strip all non-alphanumeric characters.
2. Convert all letters to their respective digits (not sure what you'd do with Q and Z, but I've never seen them in a real number, because they aren't standardized).
3. Remove the leading 1 from the number if supplied.
4. Truncate the number to 7 or 10 characters. Likely 10.
5. If there was no area code supplied, then you should apply a standard one (if this is local) or ask the user to be more specific.
boogyman
09-21-2007, 03:52 PM
I know how to define a telephone number and yah I guess i used the wrong terminology I should have used presentation rather than format....
but gah I needa take down my poll. I forgot about the (dot) and the optional (1) to dial outside your area code, and also using letters haha ... looks like I was still wrong... anyway I came up with 75 yes count it 75 unique way's of presenting a usa phone number
starting ranging in the use of... also you cannot forget (ext / x / #) for a use of a possible extension number
parethesis, dashes, spaces, nothing and combining them all
now this wasnt so much of a problem in creating the regular expression, I just would not mind using someone elses code to prevent from re-inventing the wheel. that is why I went on my search in the first place, not that i didnt know how to create it... but thank you very much for the detailed explanation.. I am sure there are many people on DD that do not know how to accurately tell between the different formats, given i couldn't find 1 script online that didnt test the whole lot.
djr33
09-21-2007, 04:28 PM
As long as there are 10 numbers present, then that's all you can really check for without calling it.
(You can be sure that the 1st and 4th digits aren't 1s.)
You could also require a certain format by example and give an error until it's fixed.
As for extensions, I'd say just use a separate field. That's not really part of the number.
you could even use 4 fields-- area code, first 3, second 4, then extension. No real way to get that wrong.
boogyman
09-21-2007, 07:36 PM
I am doing this in php, and came up with
function checkTele($num)
{
while( !preg_match('/^\d{7|10}$/', $) )
{
$num = str_replace("/[^[:digit:]]$/", "", $num);
}
}
jscheuer1
09-22-2007, 05:18 AM
As far as I know, no US number uses ### 555 ####, the 555 designation being the most popular for non-existent numbers used in fictional literature, TV, and films.
The 1 is simply the USA's country code, and is required for all long distance outside of of one's local area, but it can get complicated because some local areas include two or more area codes (the area code is the first group of three numbers). In the case of multiple area codes within a local area, all ten digits, without the 1 prefix, are required.
Letter designations in the second group of three numbers in US phone numbers (once the rule) are passe, but may still be used. Phone numbers that spell out something that is easy to remember are still quite popular in the US, especially for businesses.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.