-1

I'd like to write a custom jQuery validator method to validate a phone number and I need help with the regex for it.

It's for international phone number so it needs to accept only 10-15 digits. One more condition here is that I have a phone mask already in place: +___ ___ ___ ___ ___ (There's + and 15 counts of underscores here).

The regex should possibly ignore the +, and the underscores, and any number of digits that is between 10-15 should be accepted as a valid phone number. screenshot of the phone area field is here

Possible accepted numbers:

+2345678901_____
+23456789012____
+234567890123___
+2345678901234__
+23456789012345_
+234567890123456

Thank you for your help!

Ash K
  • 1,802
  • 17
  • 44

1 Answers1

3

The following pattern would seem to work here:

^\+(?=.{15})\d{10,15}_{0,5}$

This uses a positive lookahead to assert that exactly 15 characters, excluding the leading +, occur in the input. With that assertion in place, we can match for 10-15 digits, followed by 0-5 underscores.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360