Cyberpks
Cyberpks

Reputation: 1421

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.

Upvotes: 24

Views: 136091

Answers (9)

PatricioS
PatricioS

Reputation: 111

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

Upvotes: 0

akshay mithari
akshay mithari

Reputation: 1

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

Upvotes: 0

Jürgen Steinblock
Jürgen Steinblock

Reputation: 31723

This should do the trick:

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

Upvotes: 3

Neel Shah
Neel Shah

Reputation: 791

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

}

Upvotes: 15

Vishal Thakur
Vishal Thakur

Reputation: 1696

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

Upvotes: 2

sagar varade
sagar varade

Reputation: 61

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

Upvotes: 4

user1697238
user1697238

Reputation:

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

Upvotes: 0

Joe
Joe

Reputation: 15802

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.

Upvotes: 31

Jacob George
Jacob George

Reputation: 2627

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

Upvotes: 2

Related Questions