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.
The easiest way to match both
and
is to use alternation (
(...|...)): specify them as two mostly-separate options: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:(Though it’s probably best to explicitly demonstrate the format that you expect phone numbers to be in.)