Haidy
Haidy

Reputation: 257

Regex pattern for matching phone numbers

I want a regex-pattern for my phone number validator.

It has to allow digits, +, (, ) and -.

The restrictions are:

Hope somebody can make me a regex for this, I have looked at different generators and ended up with something like this:

/^(\\+)*(\\d+)(\\()*(\\d+)(\\))*(-)*(\\d+)$/

This does not do what I want. Some example numbers that have to be valid:

Upvotes: 1

Views: 138

Answers (2)

Artur Udod
Artur Udod

Reputation: 4743

Just pasted examples in http://gskinner.com/RegExr/, then searched existing ones in Community (just double tap each item in the list until it fits for all examples). Found this one:

^([().-\s0-9+]{2,}(?:(?::|x)[\s]*(?=(?:\d{1,4}))\d{1,4}[\s]*)?)$

Link: http://gskinner.com/RegExr/?353qd

I recommend you to use such tools when you wish to find a regex for suchpresumably popular patterns as a phone number or email address.

Upvotes: 1

Bastianon Massimo
Bastianon Massimo

Reputation: 1742

something very easy like this?

/^\+?[0-9-()]+$/

Upvotes: 1

Related Questions