Jquery – Regular Expression for Japan phone number

jqueryregex

I am trying to validate the Japan phone number using the following Regular expression

var pattern= /^[0-9-]{10,13}$/i;

It should allow only the following formats

  • 1234567890
  • 123-456-7890
  • 12-3456-7890
  • 123-4567-8901

But when I try it's returning true

var phone_pattern = /^[0-9-]{10,13}$/i;
var value='-----------';
alert(phone_pattern.test(value));

Best Answer

If you want it to be easily understandable I'd just use an alternation of the 4 possible patterns :

^(?:\d{10}|\d{3}-\d{3}-\d{4}|\d{2}-\d{4}-\d{4}|\d{3}-\d{4}-\d{4})$

This one matches your 4 phone numbers in 50 steps according to regex101.

If you want it to be a little bit more efficient at the cost of readability, you can factorize it :

^\d{2}(?:-\d{4}-\d{4}|\d{8}|\d-\d{3,4}-\d{4})$

This one matches your 4 phone numbers in 48 steps according to regex101.