August 7th, 2009, 10:51 AM
Matching phone numbers
I am trying to figure out a regexp that I found on http://stackoverflow.com/questions/1...ber-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...
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...
Originally Posted by SOURCE
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
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.
Comments on this post
August 7th, 2009, 12:30 PM
I'm mainly just trying to standardize the information that's being collected to keep the database as clean as possible.
Originally Posted by DevCoach
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
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.
(int)(area code)(local number)
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
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.