jviotti
jviotti

Reputation: 18909

Less strict phone regex

I want to validate a phone number with regular expressions in JavaScript.

I found lot of expression that match a specific pattern, such as xxx-xxx-xxx, but I'm looking for a less strict expression, that matches only numbers, and optionally -, , and + without restricting the number to a specific schema.

Upvotes: 3

Views: 127

Answers (2)

Nick
Nick

Reputation: 4362

Try this:

([0-9]{3}[-,+]?){2}[0-9]{4}

Here it is in action

Upvotes: 0

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

OK, so how about just:

/^[0-9\-,+]+$/

or, if you want to allow spaces too:

/^[0-9\-,+\s]+$/

You might also want to require that phone numbers have at least n digits, which you could do like this:

/^[\-,+\s]*([0-9][\-,+\s]*){n,}$/

where you need to replace the n with the minimum number of digits you want.

Upvotes: 3

Related Questions