Categories
phone-number regex validation

How to validate phone numbers using regex

1024

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.

2

541

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

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

24

  • 42

    The formatting code is going to be a waste of time if the numbers are allowed to come from outside the US.

    Jul 21, 2009 at 12:13

  • 28

    This is good and all, but it doesn’t validate what was entered was actually a phone number. For example, what if the user doesn’t enter the requisite 10 digits? This should be combined with good regex validation.

    Jul 29, 2010 at 13:40

  • 143

    Considering the question was about validation – this is a really really bad answer.

    – PlexQ

    Mar 30, 2012 at 17:12

  • 17

    @PlexQ I disagree. The original problem is trying to handle phone number validation because it was trying to handle all the possible formatting options. Rather than try to solve all that, take the input and “pre-strip” it of all formatting fluff until you have just the “number”. Doing this solves 2 problems – testing the result is now easy and you can now ensure that values rendered back out for display can all be formatted consistently. The first comment on this answer about the “Complicator’s Gloves” is a good read… sometimes the answer to a problem is to approach it differently.

    – scunliffe

    Mar 30, 2012 at 20:19

  • 43

    How the hell is this such a highly voted answer? This doesn’t validate anything. What’s worse is all the other phone number validation questions reference this one…

    – jlars62

    Jun 19, 2014 at 18:37


325

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 (e.g., +0.111-222.3333)? How are extensions (e.g., 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:

^(?:(?:\+?1\s*(?:[.-]\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+))?$

11

  • 7

    here it is without the extension section (I make my users enter ext in a separate field): ^(?:(?:\+?1\s*(?:[.-]\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})$

    – aarona

    May 4, 2010 at 4:37

  • 19

    Here is a version that only matches 10 digit phone numbers (not 7 digit like 843-1212): /(?:(?:\+?1\s*(?:[.-]\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})/

    Sep 7, 2010 at 19:32

  • 11

    10 digit accepts () around area code, and dosen’t allow preceeding 1 as country code (?:(?:(\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})

    – Brooke.

    Jan 9, 2011 at 5:09


  • 6

    @StevenSoroka I have had Jeffrey Friedl’s book beside me on my desk for the past two years, as regular expressions are a major part of my work. It takes a good while to really understand regular expressions. Sometimes, the readers of this site are simply looking for an existing soltuion, rather than writing their own, especially in domains with lots of corner cases, such as phone number representations.

    – Justin R.

    Mar 28, 2013 at 21:06

  • 7

    @fatcat1111 I understand that, but majority of the responses here are “me too” type of one-off regular expressions that likely don’t fit any of your corner cases. These then end up on all the websites I’m trying to use and I can’t enter my zip code or phone number or email address because someone used a half-baked regular-expression (eg: + is a valid character in email addresses). The best responses on this page point users to libraries, not to napkin-scrawled regexes.

    Apr 5, 2013 at 21:49

324

.*

If the users want to give you their phone numbers, then trust them to get it right. If they do not want to give it to you then forcing them to enter a valid number will either send them to a competitor’s site or make them enter a random string that fits your regex. I might even be tempted to look up the number of a premium rate horoscope hotline 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"

12

  • 229

    I agree with the sentiment here, but sometimes it’s nice to perform validation when the phone number is actually going to be used for something important in the interest of the user. Best example here is credit card authorization for a purchase. If the phone number is wrong, the auth might fail.

    – Pointy

    Nov 10, 2010 at 18:21

  • 60

    If the user doesn’t want to enter his phone number you can just allow the field to be optional, but is it to much to ask the user to enter a valid phone number if they are going to enter one?

    Dec 6, 2010 at 19:41

  • 13

    Also a role of validation is simply to remind people to add area codes etc that they might not otherwise remember to add, but which cannot possibly be guessed after the fact.

    Feb 23, 2011 at 0:09

  • 35

    @Pointy But regex validation won’t help you. The one and the only way to actually validate if the phone number is correct is to actually send a message to it (in case of mobile) AND make sure the user confirms using some kind of verification code. This is what you do when the number correctness is important. Everything else is just for user’s convenience to protect against some (but not all) typos and does not validate anything.

    – Alex B

    Nov 16, 2012 at 6:40

  • 16

    Cool. My phone number is 1′ OR 1=1 — PWNED. See xkcd.com/327 and owasp.org/index.php/Testing_for_SQL_Injection_(OWASP-DV-005)

    Sep 19, 2013 at 8:31