whoblitz
whoblitz

Reputation: 1065

C# regular expression for American phone numbers without hyphens or parentheses?

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

Answers (3)

Mark Byers
Mark Byers

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

Ken White
Ken White

Reputation: 125748

Try this:

^1?[^0-1][0-9]{9}$

It matches

13155551212
3155551212

It doesn't match

03155551212
10315551212
1055551212
11555551212

Upvotes: 0

Matt
Matt

Reputation: 44078

That's actually pretty straightforward: ^1?\d{10}$

Upvotes: 3

Related Questions