24

I need to validate a text field in my registration form for which I want a Regex.

The textbox will accept a mobile number like 9999999999(10 digit - starting with 7 or 8 or 9). The user may alternatively write +919999999999, where +91 is country code (+91 for India, but can accept any other like +110 or +52), or he may also write 09999999999 (first 0 for own country)

Here, the user have 3 choices,

  1. adding the mobile number with country code
  2. simply without country code or 0 prefixed
  3. With a zero prefixed in mobile number.

Though not required, my page is in asp.net, and I am using built-in regular expression validator.

1
  • 1
    Not all mobile numbers start with 7, 8 or 9. Mine starts with 4. Nor are they all 10 digits, mine is 9.
    – RobG
    Commented Oct 12, 2012 at 12:07

9 Answers 9

31

From what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.

Props to SchlaWiener in the comments for the correct limit on the country code length.

9
  • This will also match +1234567999999999 ;) Commented Oct 12, 2012 at 11:38
  • @SchlaWiener Yes - he didn't specify any kind of limit as to how long the area code can be considered valid, so I just left it as "at least one number following the +". Changing the first section to ^([0|\+[0-9]{123,456})? and replacing 123 with the minimum length and 456 with the maximum length will fix that
    – Joe
    Commented Oct 12, 2012 at 11:41
  • Ok thanks, @SchlaWiener and Joe again for update on that, Joe was correct, I didn't mentioned the max/min size for that, but I actually required it. So, cheers..
    – Cyberpks
    Commented Oct 12, 2012 at 12:27
  • You're right, he didn't specify a limit. However wikipedia does: en.wikipedia.org/wiki/List_of_country_calling_codes so it should be \d{1,5} Commented Oct 12, 2012 at 12:29
  • 1
    Regardless of any specific country code, this ^([0|\+[0-9]{1,5})?([0-9]{10})$ will also work for generic phone numbers
    – sohaiby
    Commented Jun 4, 2015 at 13:48
15

Simple regexp for Indian mobile number validation: /^[0]?[6789]\d{9}$/

Support 08888888888(Zero appending) 7878787878,8634456876,9545559877(any mobile number precede by 7,8,9 and followed by other 9 digits)

Full function

function mobileNumber(){

     var Number = document.getElementById('YOUR ELEMENT ID').value;
     var IndNum = /^[0]?[789]\d{9}$/;

     if(IndNum.test(Number)){
        return;
    }

    else{
        $('#errMessage').text('please enter valid mobile number');
        document.getElementById('profile_telephoneNumber').focus();
    }

}
1
  • 1
    Use /^[0]?[6789]\d{9}$/ since now indian mobile numbers can also start with the digit 6.
    – Shubham P
    Commented Jul 1, 2021 at 10:11
4

Try following:

function validateMobile(mobilenumber) {   
    var regmm='^([0|+[0-9]{1,5})?([7-9][0-9]{9})$';
    var regmob = new RegExp(regmm);
    if(regmob.test(mobilenumber)){
        return true;
    } else {
        return false;
    }    
}
2
  • Can you add some explanation to your answer
    – executable
    Commented Jan 9, 2019 at 10:32
  • 2
    While this code may answer the question, it is always good to explain/support it. Commented Jan 9, 2019 at 11:19
3

This should do the trick:

^(\+[\d]{1,5}|0)?[7-9]\d{9}$
1
  • for Indian mobile number following regex also works ^[6-9]\d{9}$ if (! /^[6-9]\d{9}$/.test(mobile_no)) { // Error code here } http://fiddly.org
    – Amit
    Commented Jan 2, 2019 at 7:03
2

You could also do it using string manipulation. Check this link

str = "+919999999999"
cc = str.split(str.slice(-10))[0]

The result will be +91

2

For Mobile Number Validation - Please Use this REGEX

var mobile_number = 9867345673;   // return true
var mobile_number = 1234566667;   // return false
var mob_regex = /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;
if(mob_regex.test(mobile_number)){
   return true;
} else {
   return false;
}

See Attached Screenshot for Reference.

enter image description here

0
if(document.getElementById('mobile_number').value != ""){

       var y = document.getElementById('mobile_number').value;
       if(isNaN(y)||y.indexOf(" ")!=-1)
       {
          alert("Invalid Mobile No.");
          document.getElementById('mobile_number').focus();
          return false;
       }

       if (y.length>10 || y.length<10)
       {
            alert("Mobile No. should be 10 digit");
            document.getElementById('mobile_number').focus();
            return false;
       }
       if (!(y.charAt(0)=="9" || y.charAt(0)=="8"))
       {
            alert("Mobile No. should start with 9 or 8 ");
            document.getElementById('mobile_number').focus();
            return false
       }
 }

and you can also see in more exampled link click here

http://p2p.wrox.com/javascript-how/64920-validation-phone-number-mobile-number.html

0

for generically checking phone number with its country code /^[+][0-9]{1,5}[0-9]{10}$/ you can use above regex

0

For any Chilean number use:

/^(\+?56)?((9\d{8})|(22\d{7})|(23\d{7})|(43\d{7})|(42\d{7})|(41\d{7})|(45\d{7})|(65\d{7})|(64\d{7})|(67\d{7})|(61\d{7})|(63\d{7}))$/

To use only a cellphone validation:

/^(\+?56)?(9\d{8})$/

You can check more specifications over:

https://es.wikipedia.org/wiki/Anexo:Prefijos_telef%C3%B3nicos_de_Chile https://www.entel.cl/ayudalinea/codigos_area_nacional.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.