Javascript 验证身份证是否有效!
<script language="javascript">
function isChinaIDCard(){
StrNo = document.getElementById("<%=txtCarID.ClientID%>").value;
if(StrNo.length == 15){
if(!isValidDate("19"+StrNo.substr(6,2),StrNo.substr(8,2),StrNo.substr(10,2))){return false;}
}else if(StrNo.length == 18){
if (!isValidDate(StrNo.substr(6,4),StrNo.substr(10,2),StrNo.substr(12,2))){return false;}
}else{
showLayer('popupBox',"输入的身份证号码必须为15位或者18位!");
return false;
}
if (StrNo.length==18)
{
var a,b,c
if (!isNumber(StrNo.substr(0,17))){showLayer('popupBox',"身份证号码错误,前17位不能含有英文字母!");return false;}
a=parseInt(StrNo.substr(0,1))*7+parseInt(StrNo.substr(1,1))*9+parseInt(StrNo.substr(2,1))*10;
a=a+parseInt(StrNo.substr(3,1))*5+parseInt(StrNo.substr(4,1))*8+parseInt(StrNo.substr(5,1))*4;
a=a+parseInt(StrNo.substr(6,1))*2+parseInt(StrNo.substr(7,1))*1+parseInt(StrNo.substr(8,1))*6;
a=a+parseInt(StrNo.substr(9,1))*3+parseInt(StrNo.substr(10,1))*7+parseInt(StrNo.substr(11,1))*9;
a=a+parseInt(StrNo.substr(12,1))*10+parseInt(StrNo.substr(13,1))*5+parseInt(StrNo.substr(14,1))*8;
a=a+parseInt(StrNo.substr(15,1))*4+parseInt(StrNo.substr(16,1))*2;
b=a%11;
if (b==2) //最后一位为校验位
{
c=StrNo.substr(17,1).toUpperCase(); //转为大写X
}
else
{
c=parseInt(StrNo.substr(17,1));
}
switch(b)
{
case 0: if ( c!=1 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:1");return false;}break;
case 1: if ( c!=0 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:0");return false;}break;
case 2: if ( c!="X") {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:X");return false;}break;
case 3: if ( c!=9 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:9");return false;}break;
case 4: if ( c!=8 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:8");return false;}break;
case 5: if ( c!=7 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:7");return false;}break;
case 6: if ( c!=6 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:6");return false;}break;
case 7: if ( c!=5 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:5");return false;}break;
case 8: if ( c!=4 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:4");return false;}break;
case 9: if ( c!=3 ) {showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:3");return false;}break;
case 10: if ( c!=2 ){showLayer('popupBox',"身份证好号码校验位错:最后一位应该为:2");return false;}
}
} else {//15位身份证号
if (!isNumber(StrNo)) {showLayer('popupBox',"身份证号码错误,前15位不能含有英文字母!");return false;}
}
return true;
}
function isValidDate(iY, iM, iD) {
if (iY>2009 || iY<1900 || !isNumber(iY)){
showLayer('popupBox',"输入身份证号,年度"+iY+"非法!");
return false;
}
if (iM>12 || iM<=0 || !isNumber(iM)){
showLayer('popupBox',"输入身份证号,月份"+iM+"非法!");
return false;
}
if (iD>31 || iD<=0 || !isNumber(iD)){
showLayer('popupBox',"输入身份证号,日期"+iD+"非法!");
return false;
}
return true;
}
/**
* 验证是不是数字
*/
function isNumber(oNum) {
if(!oNum) return false;
var strP=/^\d+(\.\d+)?$/;
if(!strP.test(oNum)) return false;
try{
if(parseFloat(oNum)!=oNum) return false;
}
catch(ex)
{
return false;
}
return true;
}
function showLayer(id,txt) {
document.getElementById(id).style.height=document.documentElement.scrollHeight;
document.getElementById(id).style.display = "block";
document.getElementById("txtid").innerText=txt;
document.body.style.overflow="hidden";
document.body.scrollTop=0;
}
function hideLayer(id) {
document.getElementById(id).style.display = "none";
document.body.style.overflow="visible";
}
</script>
<!--弹出层:start-->
<div class="popupBox" id="popupBox">
<iframe class="popupFrame"></iframe>
<div class="popupMask"></div>
<div class="popupContent" id="popupContent">
<h3 id="txtid"></h3>
<button onclick="hideLayer('popupBox')"></button>
</div>
</div>
<!--弹出层:end-->