Indian Phone Number Validation

Phone number validation script allows to check the Indian land line and mobile numbers on your webpage.

Features

  • Restricts alphabets and allows only numbers to be entered.
  • Validates both land line and mobile numbers with area code.
  • Provides the location details if the landline number is entered along with the code.
  • Alert message will be displayed for the invalid number.
  • Simple, responsive and easy to integrate.

Preview

Indian phone number validation script


Note: Enter '91' instead of '+91' and landline number should be entered with code.

©h

Downloads

Code

<!--Scripts by hscripts.com-->

<!--More scripts @ www.hscripts.com-->

<html>

<head>
<title>Indian Phone Number Validation in Javascript </title>
<script type="text/javascript">
var statecode = new Array("011","022","033","044","040","080","020","079","135","161","175","141","291","294","251","260","261","265","361","364","413","421","422","423","424","427","431","452","462","471","484","487","490","495","497","467","532","512","522","542","551","101","612","712","731","755","824","861","863","866","877","883","884","891","870");

var statecode2 = new Array("011","022","033","044","040","080","020","079","0135","0161","0175","0141","0291","0294","0251","0260","0261","0265","3061","0364","0413","0421","0422","0423","0424","0427","0431","0452","0462","0471","0484","0487","0490","0495","0497","0467","0532","0512","0522","0542","0551","0101","0612","0712","0731","755","0824","0861","0863","0866","0877","0883","0884","0891","0870");

var state = new Array("New Delhi","Mumbai","Kolkata","Chennai","Hyderabad","Bangalore","Pune","Ahmedabad","Dehradun","Ludhiana","Patiala","Jaipur","Jodhpur","Udaipur","Kalyan","Vapi","Surat","Vadodara","Guwahati","Shillong","Puducherry","Tiruppur","Coimbatore","Nilgiris","Erode","Salem","Tiruchirappalli","Madurai","Tirunelveli","Thiruvananthapuram","Kochi","Thrissur","Thalassery","Kozhikode","Kannur","Kasargod","Allahabad","Kanpur","Lucknow","Varanasi","Gorakhpur","Mau","Patna","Nagpur","Indore","Bhopal","Mangalore","Nellore","Guntur","Vijayawada","Tirupati","Rajahmundry","Kakinada","Visakhapatnam","Warangal");

function Check(mobnum1){
var input =document.getElementById('txtmobnum').value;if (input=='') {
document.getElementById('msg').style.color = "red";document.getElementById('msg').innerHTML = 'Enter Phone Number!';}else if (input.length=='9') {
document.getElementById('msg').style.color = "red";document.getElementById('msg').innerHTML = 'Your Number Is Not Valid';}

else{
mobnum1 = mobnum1.replace(/^\s+|\s+$/gm,'');var regex2 = /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;
var regex1 = /^0{0,1}[1-9]{1}[0-9]{1,2}[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$/;
var f3 = mobnum1[0]+mobnum1[1]+mobnum1[2];var f4 = mobnum1[0]+mobnum1[1]+mobnum1[2]+mobnum1[3];
if(mobnum1.match(regex1)){var a = statecode.indexOf(f3);var b = statecode2.indexOf(f4);if (a>=0) {if (state[a]!=null) {
document.getElementById('loc').innerHTML = 'Number Location is ' +state[a];
}
else{
document.getElementById('loc').innerHTML = '';} }

else{
if (state[b]!=null) {

document.getElementById('loc').innerHTML = 'Number Location is ' +state[b];}
else{

document.getElementById('loc').innerHTML = '';} }

document.getElementById('msg').style.color = "green";
document.getElementById('msg').innerHTML = 'Your Number Is Valid';}
else if (mobnum1.match(regex2)) {

document.getElementById('msg').style.color = "green";
document.getElementById('msg').innerHTML = 'Your Number Is Valid';
document.getElementById('loc').innerHTML = '';}
else{

document.getElementById('msg').style.color = "red";
document.getElementById('msg').innerHTML = 'Your Number Is Not Valid';
document.getElementById('loc').innerHTML = '';}}
}
function trimExtraHyphen(obj)
{
var utext =obj.value.trim();
var firstChar = utext.substring(0, utext.length);
if(firstChar == '-'){utext = utext.substring(1);}obj.value = utext.replace(/-+(?=-)/g,'');

}
function checnum(obj)
{
var ch=obj.value;var numreg = /^[0-9 \-]+$/;if (ch.match(numreg)){}
else{var df=ch.substring(0,(ch.length-1));

obj.value=df;}}
function chk(){
var sds = document.getElementById('dum');if(sds == null){alert("You are using a free package.\n You are not allowed to remove the tag.\n");
document.getElementById("maindiv").style.visibility="hidden";
}var sdss = document.getElementById("dumdiv");if(sdss == null){alert("You are using a free package.\n You are not allowed to remove the tag.\n");}
}
window.onload=chk;

</script>

<style>
.resp_code {
color: #333;font: 1em/1.3em Tahoma,Geneva,sans-serif;margin: 5px 10px 10px;overflow: auto;padding: 10px 20px;
}
input[type="button"]{

background: none repeat scroll 0 0 #468cd2;
border-bottom: 3px solid #3277bc;text-shadow: 1px 1px 0 #214d73;border: medium none;border-radius: 0.3em;color: #fff;cursor: pointer;font-weight: bold;margin: 10px 0;padding: 7px 14px;
}
.frms{

margin:0 auto;padding:10px;
-moz-border-radius:.3em;
-webkit-border-radius:.3em;
-o-border-radius:.3em;font-family:Tahoma, Geneva, sans-serif;color:#333;
font-size:.9em;line-height:1.2em;
}
.frms input[type="text"]
{
background:#fff;border:#ddd 1px solid;border-radius:.35em;
-moz-border-radius:.35em;
-webkit-border-radius:.35em;
-o-border-radius:.35em;padding:0 .5%;
margin-top:5px;margin-bottom:15px;
height:35px;}
.frms input:hover{box-shadow:#dae1e5 0px 0px 5px;-moz-box-shadow:#dae1e5 0px 0px 5px;
-webkit-box-shadow:#dae1e5 0px 0px 5px;
-o-box-shadow:#dae1e5 0px 0px 5px;}.frms input:focus{-webkit-box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);-moz-box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);
border:#9d9983 1px solid;}
body {
background: #fff none repeat scroll 0 0;font: 0.81em/150% Tahoma,Geneva,sans-serif;word-wrap: break-word;}

#msg{font-weight:bold;}

</style>

</head>

<body>

<center><b>Indian phone number validation script</b></center>
<div class='resp_code frms' align='center' id='maindiv'>

<label><font style="font-size:13px;"> Enter Phone Number : </font></label>
<input type="text" maxlength='12' name="txtmobnum" id='txtmobnum' onkeyup="checnum(this),trimExtraHyphen(this)"/>

<input type="button" value="Check" name="mobnum" onclick="Check(txtmobnum.value)" align='center'/><br>

<div id='msg' align='center'></div><br>

<div id='loc'></div>

<p>Note: Enter '91' instead of '+91' and landline number should be entered with code.</p>
<div id="dumdiv" align="center" style=" font-size: 10px;color: #dadada;"><a id="dum" style="padding-right:0px; text-decoration:none;color: #dadada;text-align:center;" href="https://www.hscripts.com">&copy;h</a>
</div>

</div>

</body>

</html>


<!--Scripts by hscripts.com-->
  • Release Date - 04-06-2015
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to support@hscripts.com

Usage

  • Copy and paste the code in a file.
  • Run the file in your browser.
  • Enter the landline number with area code to validate and find the location.
  • Enter the numbers and click check to validate the Indian phone number.
  • Maximum of 12 numbers is accepted.

License

Related Scripts

Free Javascript Scripts


Ask Questions

Ask Question