473,836 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

US phone number validation

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
10 14309
>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
^(\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_a ll i think)

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

Apr 8 '06 #3
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
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
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*******@attgl obal.net
=============== ===
Apr 13 '06 #6
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
In article <DY************ ********@comcas t.com>,
Jerry Stuckle <js*******@attg lobal.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
Tim Streater wrote:
In article <DY************ ********@comcas t.com>,
Jerry Stuckle <js*******@attg lobal.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*******@attgl obal.net
=============== ===
Apr 13 '06 #9
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($r gx, $userinput)){
$this->addError('Th is 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6266
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your full name is required. Please enter your full name!'); validity = false; frmComments.name.focus();
5
3532
by: Kamaluokeakua | last post by:
I have to write an application that deals with clients in multiple countries. The addresses, phone numbers, country id and currency information has to be stored into a database that allows for the format of any country. I would like to be able to automatically display the information in the format of their country based on the information gathered from the Country field in the database. Is there any white paper, or book or site that has...
4
11742
by: Srinivas Kollipara | last post by:
Hey guys, i have a small question in C#. I have a windows form which has Address fields, zip code and phone number fields. it is working fine with U.S. but i need to modify that for all the other countries. is there any way i can validate zipcode and phone numbers.... Thanks in advance. bye Srinivas
2
3280
by: Jeff Kiesel | last post by:
Has anyone used three textboxes for phone number input and successfully validated it? One textbox for area code, one for exchange, one for number. (yes, we're only doing US numbers) :o)
2
2249
by: Sharon Traineanu via .NET 247 | last post by:
I'm using C#.net. I have a phone number in three asp textboxes. I'd like to make the phone number required, and validate to make sure only numbers have been entered. My main problem is I can't find a way to have all three textboxes use the same validator and not display 3 error messages. -------------------------------- From: Sharon Traineanu -----------------------
6
27694
by: Michael R | last post by:
I haven't found anything that would help me to understand the correct syntax for having a certain number of digits phone number validation rule. My input mask for this field is: \000\-0000000;; When I set the following validation rule: 9999999999 or =9999999999 it doesn't work correctly and it allows short numbers. How should I compose it correctly. Thanks, Michael.
3
2250
by: ryushinyama | last post by:
I am wanting to remove 1's that Canadian customers put in front of their numbers because when FedEx imports them for shipping they leave the 1 and cut off the last number. Other countries numbers don't get formatted the same so they are not an issue. The code below was an attempt to remove a one at the beginning if present at all. Which should get the job done but I don't know if there are countries out there that need to start in the...
2
3093
by: ajaymohank | last post by:
Hello friends............ i am trying to get a validation for email and phone number. in my php project i am doing the validation using java script. but i am not getting validations for email and phone number correctly... please give a solution for this..
1
2106
by: thulaseeram | last post by:
Hi to All, I am using general javascript function isNAN to validate whether entered input is number or not it is doing ok. But it should take space in between code number and phone number. for Example: now it it is taking 078245378 (not allowing spaces, This is existing validation) but it should allow space like 078 245378. Please can any one help me how to validate this(i mean how to...
0
10838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10544
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10585
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9369
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7788
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6977
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3111
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.