By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,659 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,659 IT Pros & Developers. It's quick & easy.

US phone number validation

P: n/a
I'm still working on validating the phone numbers that are entered on a
form but have come across a problem I don't understand how to fix. I can
handle most instances when it's in regular US formats (555-555-5555 or
(555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
ten consecutive numbers with nothing else (5555555555). Not only does it
not validate, but it shows as 2147483647 when I call the variable to
check to see how it is set. I know I could break the form up into three
values as in <input name="phone[area]"> <input name="phone[code]"> and
<input name="phone[num]"> to eliminate that happening but I don't like
the look of that and I know there's a better way that I haven't found.

I tried googling for information on a preg_replace statement that could
do what I need (check the phone number and add a space between the 3rd
and 4th and the 6th and 7th digits if there isn't one there already) but
I haven't been successful.

What do I need to do to handle this?
Apr 8 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
>I'm still working on validating the phone numbers that are entered on a
form but have come across a problem I don't understand how to fix. I can
handle most instances when it's in regular US formats (555-555-5555 or
(555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
ten consecutive numbers with nothing else (5555555555). Not only does it
not validate, but it shows as 2147483647 when I call the variable to
check to see how it is set.
Telephone numbers are not integers. They are strings. In particular,
leading zeroes count. Don't treat them as numbers. Don't print
them using numeric formats. A 10-digit phone number may not fit in
a 32-bit integer. Although you won't find US phone numbers beginning
with zero, on international calls from the US it does matter whether
you dial a leading zero or not. Also, there's a difference between
817-555-0001 and 817-555-1.

US telephone numbers also occasionally have extensions, which makes
them longer than 10 digits.
I know I could break the form up into three
values as in <input name="phone[area]"> <input name="phone[code]"> and
<input name="phone[num]"> to eliminate that happening but I don't like
the look of that and I know there's a better way that I haven't found.
My preference would be to STORE it as a digit string (varchar) and
display it in some standardized format I like. On input, it's probably
best to first strip out all the non-digits and then validate it.
I tried googling for information on a preg_replace statement that could
do what I need (check the phone number and add a space between the 3rd
and 4th and the 6th and 7th digits if there isn't one there already) but
I haven't been successful.


Gordon L. Burditt
Apr 8 '06 #2

P: n/a
^(\D*)?(\d{3})(\D*)?(\d{3})(\D*)?(\d{4})$

if my regex memory serves correctly:
That will match xYYYxYYYxYYYY, where each x is a non-digit and is
optional, and Y is a digit. Group 2, 4, and 6 are the area code,
prefix, and suffix, respectively.

1234567890 - match
(123) 456-7890 - match
A123BCD4567890 - match
123-456-7890 - match

Another option is to strip all non-digits and see if its 10 characters
long:
regex: \D, with the global modifier on (preg_replace_all i think)

Store phone numbers as strings with all non-digits stripped out, then
display them how you like.

Apr 8 '06 #3

P: n/a
NC
JackM wrote:

I'm still working on validating the phone numbers .... What do I need to do to handle this?


Strip every non-digit from the string containing the phone number and
make sure that there are exactly ten symbols left; additionally, in the
U.S., the first digit cannot be 0 or 1.

Cheers,
NC

Apr 8 '06 #4

P: n/a
Are you sure about that? Is 1 not the US/CA country code? I can dail
full 10 digit numbers (1xxxyyyzzzz) without any problem in the US

Apr 13 '06 #5

P: n/a
Richard Levasseur wrote:
Are you sure about that? Is 1 not the US/CA country code? I can dail
full 10 digit numbers (1xxxyyyzzzz) without any problem in the US


Richard,

That's 11 digits :-)

And it's general practice in the U.S. to leave the '1' off the phone number.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 13 '06 #6

P: n/a
NC
Richard Levasseur wrote:

Are you sure about that? Is 1 not the US/CA country code?
Not exactly. When you call from outside North America, 1 is indeed the
U.S. country code. When you call from the U.S. and the first number
you dial is 1, it's a signal for the telephone exchange to switch from
local to long-distance dialing. The exchange assumes that the 10
digits after the 1 are a three-digit area code followed by a
seven-digit phone number. The single system of area codes covers the
U.S., Canada, Mexico, and several island nations in the Caribbean.
Essentially, if you are calling from, say, New York, dialing
1xxxyyyzzzz can get you to Chicago, Montreal, Mexico City, or Grand
Cayman.

Similarly, 0 signals the caller's intent to place an international
call. This is why area codes do not start with either 1 or 0.
I can dail full 10 digit numbers (1xxxyyyzzzz) without any
problem in the US


Yes, but this is 11 digits. :)

Cheers,
NC

Apr 13 '06 #7

P: n/a
In article <DY********************@comcast.com>,
Jerry Stuckle <js*******@attglobal.net> wrote:
Richard Levasseur wrote:
Are you sure about that? Is 1 not the US/CA country code? I can dail
full 10 digit numbers (1xxxyyyzzzz) without any problem in the US


Richard,

That's 11 digits :-)

And it's general practice in the U.S. to leave the '1' off the phone number.


I take it you mean, when saying that, the number as written down. I'm
not sure I agree. When I lived there I kept seeing and hearing ads that
said as it might be "Call one-eight-hundred .... now for your ...".

-- tim
Apr 13 '06 #8

P: n/a
Tim Streater wrote:
In article <DY********************@comcast.com>,
Jerry Stuckle <js*******@attglobal.net> wrote:

Richard Levasseur wrote:
Are you sure about that? Is 1 not the US/CA country code? I can dail
full 10 digit numbers (1xxxyyyzzzz) without any problem in the US


Richard,

That's 11 digits :-)

And it's general practice in the U.S. to leave the '1' off the phone number.

I take it you mean, when saying that, the number as written down. I'm
not sure I agree. When I lived there I kept seeing and hearing ads that
said as it might be "Call one-eight-hundred .... now for your ...".

-- tim


Tim,

Yes, when you tell someone to call a long distance number, you generally say
"one eight hundred". However, in forms it's generally not used.

For instance - here in the MD side of Washington, DC, I can call anyone in the
202 area code as a local call. I can also call some of the 301, 240 and 703
area codes as local, while others are long distance. I need to dial 1 for those
which are long distance, but do not dial 1 for those which are local.

Someone on the Virginia side of DC has a different combination, and those in DC
a still different one. Do you dial 1 or not?

So it has become the general procedure to document numbers without the '1' and
let the dialer figure out whether they need to dial '1' or not. The only
exception (which is what I think you're referring to) is free area codes such as
800, 877, 866, etc., which are always long distance. Then you'll hear on the TV
and radio "Dial one-eight-hundred...".
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 13 '06 #9

P: n/a
JackM wrote:
I'm still working on validating the phone numbers that are entered on a
form but have come across a problem I don't understand how to fix. I can
handle most instances when it's in regular US formats (555-555-5555 or
(555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
ten consecutive numbers with nothing else (5555555555). Not only does it
not validate, but it shows as 2147483647 when I call the variable to
check to see how it is set. I know I could break the form up into three
values as in <input name="phone[area]"> <input name="phone[code]"> and
<input name="phone[num]"> to eliminate that happening but I don't like
the look of that and I know there's a better way that I haven't found.

I tried googling for information on a preg_replace statement that could
do what I need (check the phone number and add a space between the 3rd
and 4th and the 6th and 7th digits if there isn't one there already) but
I haven't been successful.

What do I need to do to handle this?


This regex has worked pretty well for me. You can comment out the last
seperator and extension line if you don't need that.

$rgx = '/^'; //beginning of string
$rgx .= '(?:\([2-9]\d{2}\)\ ?'; //either (200-999) with optional space
$rgx .= '|'; //or
$rgx .= '[2-9]\d{2}[- \.]?)'; //200-999 with optional seperator '- .'
$rgx .= '[2-9]\d{2}'; //middle 4 digits 200-999
$rgx .= '[- \.]?'; //optional seperator '- .'
$rgx .= '\d{4}'; //last 4 digits 0000-9999
$rgx .= '[- \.]?'; //optional seperator '- .'
$rgx .= '(?:x|ext)?\.?\ ?\d{0,5}'; //optional extension
$rgx .= '$/'; //end of string

if (!preg_match($rgx, $userinput)){
$this->addError('This must be a valid phone number.');
}

If your variable is cast as an integer then the largest integer value
PHP can handle is 2147483647. PHP generally automatically switches to
float representations if your value is 2^32 or larger.[1] I store it as
a string to avoid problems of that sort...
[1]<url:http://us3.php.net/manual/en/language.types.integer.php>

Apr 14 '06 #10

P: n/a
NC wrote On 04/13/2006 12:02 PM:
Richard Levasseur wrote:
Are you sure about that? Is 1 not the US/CA country code?


Not exactly. When you call from outside North America, 1 is indeed the
U.S. country code. When you call from the U.S. and the first number
you dial is 1, it's a signal for the telephone exchange to switch from
local to long-distance dialing. The exchange assumes that the 10
digits after the 1 are a three-digit area code followed by a
seven-digit phone number.


Almost, but it is a bit more complex then that. If you start your
number with '10', the next three digits are then assumed to be the
actual long distance carrier that you wish to use for placing a long
distance call. So the number string becomes 15 digits. Also, this only
works from a POTS landline. It could be used in a database to indicate
that it's cheaper to use AT&T, say, in one particular special example,
though it probably wouldn't be useful enough to figure in. Nonetheless,
I have had people that have fed me phone numbers in that crazy format
before and it was confusing. (Strange people.)

- Mike
Apr 15 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.