22

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.

Cyberpks
  • 1,401
  • 6
  • 21
  • 51
  • 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 Oct 12 '12 at 12:07

7 Answers7

27

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.

Joe
  • 15,669
  • 4
  • 48
  • 83
  • This will also match `+1234567999999999` ;) – Jürgen Steinblock Oct 12 '12 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 Oct 12 '12 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 Oct 12 '12 at 12:27
  • You're right, he didn't specify a limit. However wikipedia does: http://en.wikipedia.org/wiki/List_of_country_calling_codes so it should be `\d{1,5}` – Jürgen Steinblock Oct 12 '12 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 Jun 04 '15 at 13:48
  • In `^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$` regex, I don't think `[` is needed before `0` at start – Shridhar Sagari Apr 09 '19 at 11:55
  • may not be related to the question, if using bootstrap can handle this in html without extra code like this – Naga Nov 01 '20 at 11:13
  • @Naga, it has nothing to do with bootstrap. The `type` and `pattern` are HTML attributes for `` tag and will work regardless. – Cyberpks Nov 03 '20 at 05:50
  • @Cyberpks I agree, sorry for my English, what I meant was without any javascript code can use the pattern attribute in HTML5 rather – Naga Nov 03 '20 at 07:50
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();
    }

}
Neel Shah
  • 791
  • 1
  • 6
  • 17
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;
    }    
}
Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27
3

This should do the trick:

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

http://fiddle.re/av1k

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 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](http://fiddly.org/f8521/1/what-will-be-the-regular-expression-for-indian-mobile.html) – Amit Jan 02 '19 at 07: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

Jacob George
  • 2,559
  • 1
  • 16
  • 28
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

Vishal Thakur
  • 1,564
  • 16
  • 25
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

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228