Reputation: 1065
I'm looking for something that will validate the following:
but NOT validate:
Basically it should validate 10 or 11 digit phone numbers that can optionally begin with 1 and don't contain spaces, hyphens or parentheses. The area code should not be optional either.
How can I do this?
Upvotes: 1
Views: 2926
Reputation: 839114
For something simple you could try this:
"^1?[0-9]{10}$"
Or slightly better:
"^1?[2-9][0-9]{9}$"
But this still matches incorrectly for some situations. For a better approach, see this answer:
Depending on the programming language you are using may want to use \d
instead of [0-9]
. But please be aware that in C# \d
can match any digits as defined by the Unicode standard. So it can include Chinese numerals and other numeric characters that are illegal characters in a phone number. However [0-9]
works everywhere, even in Unicode aware languages.
Upvotes: 1
Reputation: 125748
Try this:
^1?[^0-1][0-9]{9}$
It matches
13155551212
3155551212
It doesn't match
03155551212
10315551212
1055551212
11555551212
Upvotes: 0