Regular expression to validate US phone numbers? [duplicate]
Asked Answered
P

1

56

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.

Punster answered 19/3, 2012 at 19:0 Comment(2)
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.Othilie
This may be a duplicate, but please link to the previous answers before marking the post.Stalag
P
92

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

Pelham answered 19/3, 2012 at 19:4 Comment(12)
The above expression gives success for (000) 000-0000 how can we avoid that?Precondemn
@HaBo: Why would we want to avoid that?Pelham
since it is not a valid phone number.Precondemn
@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].Pelham
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-0000Precondemn
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*))?Tham
I ended up with ^\([2-9]{3}\)\s?[0-9]{3}-[0-9]{4}$Nympha
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*Prunella
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.Swarts
I've used ^\([2-9][\d]{2}\) [\d]{3}-[\d]{4}$ for validation which covers all the case. You can check it here - regex101.com/r/Bc3itt/2.Krill
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.Hopple
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 habitConsultant

© 2022 - 2024 — McMap. All rights reserved.