Reputation: 63
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
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
Reputation: 263933
this will work based on the two examples you gave,
^\d{2,3}\-\d{3}-\d{7}$
Upvotes: 2