55

Possible Duplicate:
A comprehensive regex for phone number validation
Validate phone number with JavaScript

I'm trying to write a regular expression to validate US phone number of format (123)123-1234 -- true 123-123-1234 -- true

every thing else in not valid.

I came up something like

 ^\(?([0-9]{3}\)?[-]([0-9]{3})[-]([0-9]{4})$

But this validates, 123)-123-1234 (123-123-1234

which is NOT RIGHT.

Community
  • 1
  • 1
Priya
  • 891
  • 3
  • 11
  • 24
  • 16
    I think it's a lot friendlier to let people type in their phone numbers any way they want. Just strip out non-digits and insist on 10 digits being left after that. – Pointy Mar 19 '12 at 19:14
  • 1
    This may be a duplicate, but please link to the previous answers before marking the post. – mindtonic Mar 24 '15 at 12:23

1 Answers1

90

The easiest way to match both

^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$

and

^[0-9]{3}-[0-9]{3}-[0-9]{4}$

is to use alternation ((...|...)): specify them as two mostly-separate options:

^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$

By the way, when Americans put the area code in parentheses, we actually put a space after that; for example, I'd write (123) 123-1234, not (123)123-1234. So you might want to write:

^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$

(Though it's probably best to explicitly demonstrate the format that you expect phone numbers to be in.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • The above expression gives success for (000) 000-0000 how can we avoid that? – HaBo Nov 26 '13 at 21:29
  • 5
    @HaBo: Why would we want to avoid that? – ruakh Nov 26 '13 at 21:33
  • 5
    since it is not a valid phone number. – HaBo Nov 26 '13 at 21:37
  • 35
    @HaBo: A regex can't detect *valid* phone numbers, only *validly formatted* phone numbers. That said, no U.S. area codes begin with `0` or `1`, so if you wanted, you could change the initial `[0-9]{3}` to `[2-9][0-9][0-9]`. – ruakh Nov 26 '13 at 23:31
  • Yeah I was looking for such thing. So will ^(\([2-9]{1}[0-9]{2}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$ works for (200) 000-0000 – HaBo Nov 27 '13 at 02:36
  • This doesn't work for me. here is what worked with me: 1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))? – Mostafa Jan 26 '16 at 14:51
  • I ended up with `^\([2-9]{3}\)\s?[0-9]{3}-[0-9]{4}$` – Joaquin Iurchuk Feb 01 '17 at 18:13
  • 11
    @joaquin: you are making a mistake here! none of the 3 digits of the area code will be able to contain 0 or 1, which means all LA and all NYC will be rejected! (310 and 212) – FlorianB Apr 16 '17 at 01:51
  • US phone number has more validation like it won't start with 0 can not have all 0 I think this is more robust for US phone \D*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4})\D* – Avijit Chakraborty May 10 '19 at 15:55
  • Another set of problematic numbers you might want to filter out in the US are prefixes such as 211, 311, 411, 511, 611, 711, 811, 911 which are reserved for special use by the government and phone companies. – Bob Holden Dec 18 '20 at 23:22
  • I've used `^\([2-9][\d]{2}\) [\d]{3}-[\d]{4}$` for validation which covers all the case. You can check it here - https://regex101.com/r/Bc3itt/2. – Abhishek Jan 21 '21 at 09:33
  • How about this : ^[2-9]{1}[0-9]{2}-[0-9]{3}-[0-9]{4}$ since USA code doesn't start with 0 & 1, check 1st digit to match 2-9, rest 2 digit to match 0-9. I consider having hyphen between digits while saving in DB to keep only 1 format. – Vivek Tankaria Oct 13 '21 at 13:26
  • you should use the regex that matches any of the users possible inputs and not ask them to format things manually unless they just do so out of habit – ICW Jul 20 '23 at 19:53