-1

I want to create a regular expression for validating phone numbers.

For example:
+381 493 34949499

+381 - 1-4 numbers
493 - 2-3 numbers
34949499 - 5-15 numbers

'/^\+\d{3} \(\d{2,3}\) \d{5,16}$/')

I trid something like this but I got an error in +381 (country code).

Andrew
  • 4,953
  • 15
  • 40
  • 58
user3827056
  • 63
  • 1
  • 1
  • 4

2 Answers2

0

Better use a proper module, have Number::Phone module to validate phone numbers.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

Probably you should not build that yourself, as there is a huge amount of different writing styles for phone numbers and I myself often get annoyed by services which implemented that stuff by theirselves. See here (Wikipedia) and check out some people who already put some work in there: Regexlib. To have some general advice: I would rather use \b as word boundaries instead of ^ and $ which are line boundaries. Probably you would want to use a tool like Regexr or Rubular to test your regular expressions first. Regexr also coems with examples for different stuff.

Geru
  • 624
  • 1
  • 7
  • 20