A comprehensive regex for phone number validation

I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it would handle international formats, but it must handle US formats, including the following:

  • 1-234-567-8901
  • 1-234-567-8901 x1234
  • 1-234-567-8901 ext1234
  • 1 (234) 567-8901
  • 1.234.567.8901
  • 1/234/567/8901
  • 12345678901
  • I'll answer with my current attempt, but I'm hoping somebody has something better and/or more elegant.


    Better option... just strip all non-digit characters on input (except 'x' and leading '+' signs), taking care because of the British tendency to write numbers in the non-standard form +44 (0) ... when asked to use the international prefix (in that specific case, you should discard the (0) entirely).

    Then, you end up with values like:

     12345678901
     12345678901x1234
     345678901x1234
     12344678901
     12345678901
     12345678901
     12345678901
     +4112345678
     +441234567890
    

    Then when you display, reformat to your hearts content. eg

      1 (234) 567-8901
      1 (234) 567-8901 x1234
    

    It turns out that there's something of a spec for this, at least for North America, called the NANP.

    You need to specify exactly what you want. What are legal delimiters? Spaces, dashes, and periods? No delimiter allowed? Can one mix delimiters (eg, +0.111-222.3333)? How are extensions (eg, 111-222-3333 x 44444) going to be handled? What about special numbers, like 911? Is the area code going to be optional or required?

    Here's a regex for a 7 or 10 digit number, with extensions allowed, delimiters are spaces, dashes, or periods:

    ^(?:(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})(?:s*(?:#|x.?|ext.?|extension)s*(d+))?$
    

    .*
    

    If the user wants to give you his phone number, then trust him to get it right. If he does not want to give it to you then forcing him to enter a valid number will either send him to a competitor's site or make him enter a random string that fits your regex. I might even be tempted to look up the number of a premium rate sex line and enter that instead.

    I would also consider any of the following as valid entries on a web site:

    "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"
    
    链接地址: http://www.djcxy.com/p/30566.html

    上一篇: ASP.Net MVC错误验证

    下一篇: 一个全面的电话号码验证正则表达式