#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Coast, MS
    Posts
    278
    Rep Power
    43

    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...

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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

    Comments on this post

    • prometheuzz agrees
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Coast, MS
    Posts
    278
    Rep Power
    43
    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 01:37 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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

IMN logo majestic logo threadwatch logo seochat tools logo