runningmark runningmark - 5 months ago 10
Javascript Question

regex expression in javascript not working as expected

we are trying to find if the given string is a valid indian mobile number or not

valid indian mobile number


  1. starts with
    7 or 8 or 9

  2. followed by 9 same or different numbers



here is my JavaScript for matching it, but unfortunately it returns false even when number is correct

var mobile_number = $('#number').val();
var mobile_regex = new RegExp('/^[789]\d{9}$/');
if(mobile_regex.test(mobile_number) == false) {
console.log("not valid");
} else {
console.log("valid");
}

Answer

You can try this

   var mobile_number = $('#number').val();
   var mobile_regex = new Regex("^[7-9][0-9]{9}$")
   if(mobile_regex.test(mobile_number) == false) {
   console.log("not valid");
   } else {
    console.log("valid");
   }