0

I am trying to validate a mobile number of 11 digit , where the 1st 3 digit will be 019 , 018 , 017 , 016 . How can I check this that the first 3 digit will be fall among the four criteria??

I can validate if the number is a character or is it less than 11 digit using the code bellow.Now the problem is I want to check If the 1st 3 digit fall into the four criteria( 019 , 018 , 017 , 016)

var phoneno = /^\d{11}$/;  
  if(inputtxt.value.match(phoneno))  
  {  
      return true;  
  } 
SOURAV
  • 314
  • 3
  • 4
  • 15

3 Answers3

2

For your specific case you could simply add a group () with the desired delimited | values:

/^(?=019|018|017|016)\d{11}$/

01711122233  // GOOD //  Has length 11 and starts with 017
017111222333 // BAD  //  Has length 12
11711122233  // BAD  //  Does not starts with either one from group

^ assert position at start of the string
(?=019|018|017|016) Positive Lookahead - Assert that the regex below can be matched
1st Alternative: 019
019 matches the characters 019 literally
2nd Alternative: 018
018 matches the characters 018 literally
3rd Alternative: 017
017 matches the characters 017 literally
4th Alternative: 016
016 matches the characters 016 literally
\d{11} match a digit [0-9]
Quantifier: {11} Exactly 11 times
$ assert position at end of the string

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1
var allowedFirstDigits = ["019", "018", "017", "016"];
var firstDigits = phoneNumber.substring(0, 3);
if (allowedFirstDigits.indexOf(firstDigits) < 0) {
    // Bad number
}
else {
    // First threee digits OK
}
christophetd
  • 3,834
  • 20
  • 33
1

You could use the regular expression: /^01[6-9]\d{8}$

MT0
  • 143,790
  • 11
  • 59
  • 117