How to validate phone numbers based on country codes in Javascript.
For Example: India-starts with 91-xxxxxxxxxx
, UK-44-xxxxxxxxxx
, SZ-268-22xxxxxx
and so on. Like this it should validate other countries country code with phone number which I will enter.
Below is the code which I tried but it is only for one country. Parallelly how to do validate with other countries country code.
function validateMobNo(mobno){
var mobno2;
var flag=false;
var mlen= mobno.length;
//alert(mobno.substr(3,mobno.length-3));
if(mobno.charAt(0)!='+' && mlen==10){
mobno2="+91-"+mobno;
alert("1>>your mobile wants to be in "+mobno2);
flag=true;
}
else if(mobno.charAt(0)=='+'){
if(mobno.substr(0,3)=='+91' && mobno.length==13){
mobno2=mobno.substr(0,3)+"-"+mobno.substr(3,mobno.length-3);
alert("2>>your mobile wants to be in "+mobno2);
flag=true;
}
}
else if(mobno.indexOf("-")<0&&mobno.length==12 && mobno.substr(0,2)=='91'){
mobno2=mobno.substr(0,2)+"-"+mobno.substr(3,mobno.length-2);
alert("3>>your mobile wants to be in "+mobno2);
flag=true;
}
else
alert("Please correct your mobile No");
if(flag==true)
document.mobvalidate.mobno.value=mobno2;
else
document.mobvalidate.mobno.focus()
return flag;
}
How to validate phone numbers based on country codes in Javascript.
For Example: India-starts with 91-xxxxxxxxxx
, UK-44-xxxxxxxxxx
, SZ-268-22xxxxxx
and so on. Like this it should validate other countries country code with phone number which I will enter.
Below is the code which I tried but it is only for one country. Parallelly how to do validate with other countries country code.
function validateMobNo(mobno){
var mobno2;
var flag=false;
var mlen= mobno.length;
//alert(mobno.substr(3,mobno.length-3));
if(mobno.charAt(0)!='+' && mlen==10){
mobno2="+91-"+mobno;
alert("1>>your mobile wants to be in "+mobno2);
flag=true;
}
else if(mobno.charAt(0)=='+'){
if(mobno.substr(0,3)=='+91' && mobno.length==13){
mobno2=mobno.substr(0,3)+"-"+mobno.substr(3,mobno.length-3);
alert("2>>your mobile wants to be in "+mobno2);
flag=true;
}
}
else if(mobno.indexOf("-")<0&&mobno.length==12 && mobno.substr(0,2)=='91'){
mobno2=mobno.substr(0,2)+"-"+mobno.substr(3,mobno.length-2);
alert("3>>your mobile wants to be in "+mobno2);
flag=true;
}
else
alert("Please correct your mobile No");
if(flag==true)
document.mobvalidate.mobno.value=mobno2;
else
document.mobvalidate.mobno.focus()
return flag;
}
Share
Improve this question
edited Jul 18, 2017 at 19:28
SLePort
15.5k3 gold badges39 silver badges45 bronze badges
asked Nov 16, 2016 at 9:31
Shivanand L.CShivanand L.C
1052 gold badges3 silver badges10 bronze badges
3
- The simplest way would be to have an object which contains all the regex patterns to test the various different countries' phone number formats, keyed by the country code. Using string manipulation for this is going to make it a lot more plicated, and more brittle, than it needs to be – Rory McCrossan Commented Nov 16, 2016 at 9:34
- Someones already done the hard work for you this int-tel-input jQuery plugin would do the trick. – R. Chappell Commented Nov 16, 2016 at 9:37
- I have used this library in the past. It is well documented. Don't re-invent the wheel. github./googlei18n/libphonenumber – ppovoski Commented Nov 16, 2016 at 10:07
1 Answer
Reset to default 3Using some
method to loop over each country code and evaluates with regular erxpressions:
country_codes=['91-', 'UK-44-', 'SZ-268-22']
phone="91-123456789";
is_valid_number = country_codes.some(elem => phone.match('^' + elem));
console.log(is_valid_number );