-2

I would like to validate mobile number field. I have tried many regular expression based on my requirement

Matching cases

  1. (923)(423)(34455)
  2. 945 443 4442
  3. 919774744323
  4. +92 332 45 666
  5. 9144565
  6. 91-877-3655

I need a regular expression based on my matching cases.

JohnMathew
  • 508
  • 1
  • 5
  • 21
  • Does the numbers need to be valid? For example: If there is a wrong prefix number, should it match if the format is correct? (for example `+999 99 999`) – Julio Mar 13 '19 at 11:12

1 Answers1

0

Try with this:

^[+]?(?:\d+|(?:\(\d+\)){3,}|\d+(?:(?: +|-)\d+){2,})$

You have a demo here

Explained:

^    # begin of line
    [+]?  # optinally start by +
    (?:   # group of possible options
        \d+ |         # first option: all numbers
        (?:\(\d+\)){3,} |    # second option: group of numbers withn parenthesis (min=3)
        \d+(?:(?: +|-)\d+){2,}  # third option: group of numbers separated by spaces or dash (min=3)
    )     # end of group of options
$    # end of line
Julio
  • 5,208
  • 1
  • 13
  • 42