<html>
<head>
<title>
大陆身份证号验证
</title>
<script>
function checkDlIdNumber(){
var idNumber = document.getElementById("idNumber").value;
if(isStandard(idNumber)){
alert("身份证号符合格式!");
}else{
alert("请输入正确的身份证号!");
}
}
function isStandard(idNumber){
if (idNumber.length < 15 || idNumber.length == 16 || idNumber.length == 17 || idNumber.length > 18){
return false;
}
var result;
if (idNumber.length == 18){
result = idNumber.substring(0, 17);
}else{
result = idNumber.substring(0, 6) + "19" + idNumber.substring(6, 12);
}
if (!isNumber(result)){
return false;
}
var strBirthDay = result.substr(6, 4) + "-" + Number(result.substr(10, 2)) + "-" + Number(result.substr(12, 2));
var d = new Date(strBirthDay.replace(/-/g, "/"));
var nowTime = new Date();
var age = nowTime.getFullYear() - result.substr(6, 4);
if (strBirthDay != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())){
return false;
}
var lastArr = 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, totalResult = 0;
for (k = 0; k < 17; k++){
totalResult = totalResult + parseInt(result.substr(k, 1)) * Wi[k];
}
var modValue = totalResult % 11;
var lastNum = lastArr[modValue];
result = result + lastNum;
if ((idNumber.length == 18) && (idNumber.toLowerCase() != result)){
return false;
}
return true;
}
function isNumber(str) {
if (str.length > 0) {
var reg = //D/;
return str.match(reg) == null;
} else {
return true
}
}
function clearValue(the){
if(the.value == '请输入身份证号...'){
the.value='';
the.style.cssText = "color:#000000;";
}
}
function returnValue(the){
if(the.value == ''){
the.value='请输入身份证号...';
the.style.cssText = "color:#cccccc;";
}
}
</script>
</head>
<body>
<input id="idNumber" type="text" value="请输入身份证号..." onfocus="clearValue(this);" onblur="returnValue(this);"/>
<input type="button" value="验证" onclick="checkDlIdNumber();"/>
</body>
<script>
//第一次加载页面的时候输入框内颜色为灰色
var idNumberObj = document.getElementById("idNumber");
if(idNumberObj.value == '请输入身份证号...'){
idNumberObj.style.cssText = "color:#cccccc;";
}
</script>
</html>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16356.html