Reputation: 237
I'm using /^\d{3}\ ?-? ?\d{3}\ ?-? ?\d{4}$/;
to validate US phone numbers. Can anyone tell me how to modify this expression to make ()
optional on the first set of numbers.
For example: it should work for (123)234-2345
, 123-345-4567
, 123 - 345 - 4567
, 123345-6789
Upvotes: 1
Views: 2640
Reputation: 74098
Area code with or without braces
(?:\(\d{3}\)|\d{3})
optional -
with optional spaces around it
(?: *- *)?
3 digits
\d{3}
and 4 digits
\d{4}
and all together now
^(?:\(\d{3}\)|\d{3})(?: *- *)?\d{3}(?: *- *)?\d{4}$
Upvotes: 2