身份证号码验证正则表达式及用法(包括:位数、规则等)
//sfz.js页面
function CheckIDCard(StrNumber) { //判断身份证号码格式函数 //公民身份号码是特征组合码, //排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码 //身份证号码长度判断 if(StrNumber.length<15||StrNumber.length==16||StrNumber.length==17||StrNumber.length>18) { return {"code":201,"errormsg":"填写的身份证号码长度不正确,请重新填写!"}; } //身份证号码最后一位可能是超过100岁老年人的X.X也可以代表是阿拉伯数字10的意思 //所以排除掉最后一位数字进行数字格式测试,最后一位数字有最后一位数字的算法 var Ai; if(StrNumber.length==18) { Ai = StrNumber.substring(0,17); } else { Ai =StrNumber.substring(0,6)+"19"+StrNumber.substring(6,9); } //调用数字判断函数IsNumeric() if(IsNumeric(Ai)==false) { return {"code":201,"errormsg":"身份证号码数字字符串不正确,请重新填写!"}; } var strYear,strMonth,strDay,strBirthDay; strYear = parseInt(Ai.substr(6,4)); strMonth = parseInt(Ai.substr(10,2)) ; strDay = parseInt(Ai.substr(12,2)); //调用日期判断函数IsValidDate() if (IsValidDate(strYear,strMonth,strDay)==false) { return {"code":201,"errormsg":"身份证号码验证失败!"}; } var arrVerifyCode = new Array("1","0","x","9","8","7","6","5","4","3","2"); var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2); var k,TotalmulAiWi=0; for (k=0; k<17;k++) { TotalmulAiWi = TotalmulAiWi + parseInt(Ai.substr(k,1)) * Wi[k]; } //alert("身份证号码最后一位的识别码是:"+TotalmulAiWi); var modValue =TotalmulAiWi % 11 ; var strVerifyCode = arrVerifyCode[modValue]; Ai = Ai+strVerifyCode; //alert("身份证号码"+StrNumber+"与正确的号码"+Ai+"一致!"); if((StrNumber.length==18)&&(StrNumber!=Ai)) { return {"code":201,"errormsg":"身份证号码验证失败!"}; } return {"code":200,"msg":"OK"}; } //数字判断函数IsNumeric()//数字判断函数IsNumeric() function IsNumeric(oNum) { return /^\d+$/.test(oNum) } function IsValidDate(iY, iM, iD) { if (iY>2200 || iY<1900 || !IsNumeric(iY)){ return false; } if (iM>12 || iM<=0 || !IsNumeric(iM)){ return false; } if (iD>31 || iD<=0 || !IsNumeric(iD)){ return false; } return true; }
用法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!--引用身份证验证的正则表达式--> <script type="text/javascript" src="../js/sfz.js"></script> <title>无标题文档</title> </head> <body> <input style="width:240px; margin-left:21px;" id="num" onblur="num0()" class="inp fl" type="text" /> <div class="fl" id="num-a"></div> </body> </html> <script type="text/javascript"> function num0() { var mail = document.getElementById("num").value; var ckresult=CheckIDCard(mail); if(ckresult.code!=200) { changdu = "<img style='margin-left:15px; margin-top:10px; float:left' src='../images/叉号.png' /><span style='color:#FF0004; font-size:14px; margin-top:10px; float:left; margin-left:10px;'>"+ckresult.errormsg+"</span>"; $("#num-a").html(changdu ); document.getElementsByClassName("btn")[0].disabled=true; } else { changdu = "<img style='margin-left:15px; margin-top:10px; float:left' src='../images/对号.png' /><span style='color:#FF0004; font-size:14px; margin-top:10px; float:left; margin-left:10px;'></span>"; $("#num-a").html(changdu ); } } </script>