写完. 15位的 地区验证. 生日验证. 15个 1 直接通过. 怎么解决.
梁秋龙(笔记本) 12:34:08
function checkIDCard(id) {
var idNum = id;
//alert(idNum);
var errors = new Array(
"验证通过",
"身份证号码位数不对",
"身份证含有非法字符",
"身份证号码校验错误",
"身份证地区非法"
);
//身份号码位数及格式检验
var re;
var len = idNum.length;
//身份证位数检验
if (len != 15 && len != 18) {
alert(errors[1])
return false;
} else if (len == 15) {
re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
} else {
re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})([0-9xX])$/);
}
var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西",
15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海",
32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西",
37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东",
45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州",
53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海",
64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门",
91: "国外"
}
var idcard_array = new Array();
idcard_array = idNum.split("");
//地区检验
if (area[parseInt(idNum.substr(0, 2))] == null) {
alert(errors[4])
return false;
}
//出生日期正确性检验
var a = idNum.match(re);
var flag="";
if (a != null) {
if (len == 15) {
var DD = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
flag = DD.getYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[5];
}
else if (len == 18) {
var DD = new Date(a[3] + "/" + a[4] + "/" + a[5]);
flag = DD.getFullYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[5];
}
if (!flag) {
//return false;
alert("身份证出生日期不对!")
return false;
}
//检验校验位
if (len == 18) {
S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
+ parseInt(idcard_array[7]) * 1
+ parseInt(idcard_array[8]) * 6
+ parseInt(idcard_array[9]) * 3;
Y = S % 11;
M = "F";
JYM = "10X98765432";
M = JYM.substr(Y, 1); //判断校验位
//检测ID的校验位
if (M == idcard_array[17]) {
return true;
//return "";
}
else {
alert(errors[3])
return false;
}
}
} else {
//return false;
return errors[2];
}
return true;
}
你们说的.我都已经看过了. 还是没有好的解决方案.
那也谢谢你们的回答.
好复杂。确实要校验正确性,直接服务端通过公开的api查询即可。,
本园内有一文可供参考吗,请楼主自行拜读
http://www.cnblogs.com/wuhuisheng/archive/2011/03/22/1991543.html