Jquery Code For Phone Number Validation - Jquery

How to validate indian phone numbers in jquery?

Snippet Code


  
Rate this page :
  [ 0 votes]

Phone number validation in jquery allows you to validate your landline and mobile numbers through online. Simple code to validate your phone numbers is given below.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <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 =$("#txtmobnum").val(); if (input=='') { $("#msg").css('color','red'); $("#msg").css('font-weight','bold'); $("#msg").text('Enter Phone Number!'); } else if (input.length=='9') { $("#msg").css('color','red'); $("#msg").css('font-weight','bold'); $("#msg").text('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) { $("#loc").text("Number Location is "+state[a]); } else{ $("#loc").text(""); } } else{ if (state[b]!=null) { $("#loc").text("Number Location is "+state[b]); } else{ $("#loc").text(""); } } $("#msg").css('color','green'); $("#msg").css('font-weight','bold'); $("#msg").text('Your Number Is Valid'); } else if (mobnum1.match(regex2)) { $("#msg").css('color','green'); $("#msg").css('font-weight','bold'); $("#msg").text('Your Number Is Valid'); $("#loc").text(""); } else { $("#msg").css('color','red'); $("#msg").css('font-weight','bold'); $("#msg").text('Your Number Is Not Valid'); $("#loc").text(""); } } } 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; } } </script> <div align='center'> <div class='resp_code frms' align='left' id='maindiv'> Enter Phone Number :</br> <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'/> <label id='msg' align='center'></label></br> <label id='loc'></label> <p>Note: Enter '91' instead of '+91' and landline number should be entered with code.</p> </div> </div>

Tags


Ask Questions

Ask Question