1

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

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

The restrictions are:

  • The + needs to be at the beginning of the output (something like: "+31(427)-103819" needs to be valid)
  • The +, (, ) and - are not required
  • The output has to end with a digit
  • No restrictions on length is required

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:

  • 190138190
  • 103-10381-390101
  • (0358)-103810381
  • (1038)1038103
  • +1081(1081)-193810
  • +1903(3913)193810
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
Haidy
  • 259
  • 6
  • 19

2 Answers2

1

something very easy like this?

/^\+?[0-9-()]+$/
Bastianon Massimo
  • 1,710
  • 1
  • 16
  • 23
1

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.

Artur Udod
  • 4,465
  • 1
  • 29
  • 58