The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Matching phone numbers
Discuss Matching phone numbers in the Regex Programming forum on Dev Shed. Matching phone numbers Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 7th, 2009, 10:51 AM
|
 |
Contributing User
|
|
Join Date: Dec 2004
Location: Coast, MS
|
|
|
Matching phone numbers
I am trying to figure out a regexp that I found on http://stackoverflow.com/questions/...mber-validation
I believe it is supposed to validate phone numbers in both U.S. and International format (accept a country code) as well as supporting extensions...
Quote: | Originally Posted by SOURCE
If you're talking about form validation, the regexp to validate correct meaning as well as correct data is going to be extremely complex because of varying country and provider standards. It will also be hard to keep up to date.
I interpret the question as looking for a broadly valid pattern, which may not be internally consistent - for example having a valid set of numbers, but not validating that the trunk-line, exchange, etc. to the valid pattern for the country code prefix.
North America is straightforward, and for international I prefer to use an 'idiomatic' pattern which covers the ways in which people specify and remember their numbers:
Code:
^(((((\d{3}))|(\d{3}-))\d{3}-\d{4})|(+?\d{2}((-| )\d{1,8}){1,5}))(( x| ext)\d{1,5}){0,1}$
The North American pattern makes sure that if one parenthesis is included both are. The international accounts for an optional initial '+' and country code. After that, you're in the idiom. Valid matches would be:
Code:
(xxx)xxx-xxxx
(xxx)-xxx-xxxx
(xxx)xxx-xxxx x123
12 1234 123 1 x1111
12 12 12 12 12
12 1 1234 123456 x12345
+12 1234 1234
+12 12 12 1234
+12 1234 5678
+12 12345678
This may be biased as my experience is limited to North America, Europe and a small bit of Asia. |
I am attempting to use this regexp in a script I am working on but when I try to pass anything through it the match fails. Even valid examples listed above in the creator's comments...
I am not very skilled with regular expressions and was hoping for some help determining if this is correct or not.
Thanks in advance for your time/replies
__________________
|

August 7th, 2009, 12:12 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
|
Try this regex: ".*"
Why do you want to validate phone numbers? I don't think they should be validated, especially on web pages. This is a bugbear of mine ever since I had to make up a random string of numbers to log into a website that insisted I enter a US-style phone number despite the fact that on the previous page I had told it I was not in the USA. Unless you are going to have the computer phone the user, there is absolutely no reason to validate the number.
I would consider these to be valid entries for the phone number field on a web page:
"123 456 7890 until 6pm, then 098 765 4321"
"123 456 7890 or try my mobile on 098 765 4321"
"ex-directory - mind your own business"
If you try and force the user to enter a valid phone number when they would rather communicate by other means then they will either enter a random number or give up and go elsewhere. If I was feeling particularly malicious I might enter the number for a premium rate sex line.
Dave
|

August 7th, 2009, 12:30 PM
|
 |
Contributing User
|
|
Join Date: Dec 2004
Location: Coast, MS
|
|
Quote: | Originally Posted by DevCoach Try this regex: ".*" |
I'm mainly just trying to standardize the information that's being collected to keep the database as clean as possible.
Given all the varying flavors of phone number syntax in the world I didn't even really know if it was possible but it seems like it should be to a point. Even if I just collect country code followed by the rest of the digits without spaces that would be better than having an array of different formats scattered throughout my data.
I may be better off collecting it in 3 parts
Code:
(int)(area code)(local number)
Then I could auto assign U.S. users int. code, let other countries enter theirs optionally, require area code and local. Then when processing strip everything out that wasn't a digit, and append all the pieces together for the DB entry.
That sound like a smarter way to go?
Edit: No, no it doesn't
Thanks for the advice  I'm going to take it
Last edited by diseased013 : August 7th, 2009 at 12:37 PM.
|

August 7th, 2009, 12:50 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
|
If you just want to standardize the database field then just strip out everything except digits - after all, that is what you will enter into the phone keypad.
Dave
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|