Gayan Fernando Gayan Fernando - 4 months ago 26
Javascript Question

Validate phone number with starting from certain number only in inputmask jquery

I am trying to validate phone number as following. After 61 it should be starting from 2 or 3 or 6 only. Examples are as follows.
61-245623463 : valid
61-345623456 : valid
61-445623423 : not valid
61-645623485 : valid

My current code is as follows. But it doesen't work.

$("#mask_aus_phone").inputmask({mask: "61-2 9999 9999"});
$("#mask_aus_phone").inputmask({mask: "61-3 9999 9999"});
$("#mask_aus_phone").inputmask({mask: "61-6 9999 9999"});


I tried as follows also. But it also doesn't work.

$("#mask_aus_phone").inputmask({mask: "61-{2,3,6} 9999 9999"});


Can someone help me to validate this.

Answer

You can feed a regex into the input mask:

$('#mask_aus_phone').inputmask('Regex', { 
    regex: "^61-[236]"
});

I used the regex ^61-[236], but you could also use a positive lookahead and assert that the number following 61- is either 2, 3, or 6:

^61-(?:(?=[236]).)+

Update:

You didn't make it explicit, but if you want to limit the total number of digits after 61- to 9, then you can try:

^61-[236]\d{8}$

Demo:

Regex101

Comments