简体   繁体   中英

Regex for Mobile Number with or without Country Code

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.

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.

Simple regexp for Indian mobile number validation: /^[0]?[789]\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();
    }

}

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;
    }    
}

This should do the trick:

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

http://fiddle.re/av1k

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

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.

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM