function checkIDCard(value){//验证中国居民身份证
var _a=/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/;//验证身份证15位
var _b=/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})([0-9xX])$/;//验证身份证18位
var l=value.length;
var re;
if (l!=15&&l!=18){
console.log('身份证号码位数不对');
return false;
}else if (l==15){
re = new RegExp(_a);
}else{
re = new RegExp(_b);
};
var idcard_array = new Array();
idcard_array = value.split("");
var options={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: "国外"}};
if(options.area[parseInt(value.substr(0, 2))] == null) { //地区检验
console.log('身份证号码位数不对');
return false;
};
var a=value.match(re);
if (a!=null){//出生日期正确性检验
if(l==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 (l==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){
console.log($(self),'身份证出生日期不对');
return false;
};
if(l==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);
//判断校验位
if (M == idcard_array[17]) {
console.log("校验通过!")
return true;
}else{
console.log('身份证号码校验错误');
return false;
};
};
}else{
console.log('身份证含有非法字符');
return false;
};
}
来源:oschina
链接:https://my.oschina.net/u/2005376/blog/674724