user2325116
user2325116

Reputation: 63

Phone Number Regex

I need a regular expression for phone number which includes country code, area code and phone number.The phone number format should be exactly like: 001-253-3478887 or 90-312-4900203. I try this but it does not work. What should be the correct format? How can I get the phone number info?

(\(\d{3}-))?\d{3}-\d{4}

Upvotes: 0

Views: 450

Answers (4)

Mike Precup
Mike Precup

Reputation: 4228

^((\d-\d{3})|(\d{1,3}))\-\d{3}-\d{7}$

This regex will match all country codes. Country codes of 1 and 1-684 won't be valid in the other answers. It will also prevent country codes such as 1-42 from being allowed, since the only time there should be a separator in a country code is when it's four digits long. If you end up getting 4 digit country codes without the separator, use the following instead:

^((\d{4})|(\d{1,3}))\-\d{3}-\d{7}$

Upvotes: 1

Santosh Panda
Santosh Panda

Reputation: 7351

You can check this out:

((\d{2,3}-))\d{3}-\d{7}

Upvotes: 0

Rasmus Søborg
Rasmus Søborg

Reputation: 3705

What about

\d{2,3}\-\d{3}\-\d{7}

Upvotes: 0

John Woo
John Woo

Reputation: 263933

this will work based on the two examples you gave,

 ^\d{2,3}\-\d{3}-\d{7}$

Upvotes: 2

Related Questions