1.简述
最近做的系统有用到实名验证的,起初对于用户身份证号只是简单地使用正则表达式进行验证,
很多无效的身份证号就成了漏网之鱼。
导致后台存表里很多无效的身份证号,随便输入用户名和身份证号就可以实名成功,这样就存在很多的弊端。
最终我们决定对身份证号进行有效性验证,严谨的剖析身份证号的组成,研究它的生成算法。
当然网上也有很多资源供做参考。https://www.jianshu.com/p/07c7ee44579c(组成规则)
我们就不详细介绍组成规则了,直接用js代码来进行有效性验证。
首先我们要明白现在我们的身份证号有两种:15位和18位,最常见的也就是18位的。但是15位的有效性验证也要兼顾。
2.面向对象的验证方法
//==面向对象方法===验证身份证号是否真实有效
function IdentityCodeValid(id){
this.aCity = { 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: "国外" }; this.iSum = 0;//计算校验位
this.pass = true;//身份证号是否有效标识
this.tip = '';//身份证号无效提示
//console.log(this.sSex) ;//地址,出生年月,性别
this.judgmentIdLength(id);//开始验证
} //检验18位身份证号是否真实有效==面向对象方法
IdentityCodeValid.prototype={
judgmentIdLength:function(code){
/**
===如果是15位则将其转换为18位再进行验证===
15位身份证号码各位的含义:
1-2位省、自治区、直辖市代码;
3-4位地级市、盟、自治州代码;
5-6位县、县级市、区代码;
7-12位出生年月日,比如670401代表1967年4月1日,与18位的第一个区别;
13-15位为顺序号,其中15位男为单数,女为双数;
与18位身份证号的第二个区别:没有最后一位的验证码。
**/
if(code.length==15){
codeArr = code.split(''); code = code.toString().substr(0,6)+"19"+code.toString().substr(6);
var last = this.computeCheckBit(code);
code = code + last;
//console.log('code=='+code);
//return this.id;
this.codeValid(code); }else{
this.codeValid(code);
}
},
computeCheckBit:function(sId){
//∑(ai×Wi)(mod 11)
//加权因子
var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校验位
var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i++) {
ai = sId[i];
wi = factor[i];
sum += ai * wi;
}
var last = parity[sum % 11];
console.log(last);
return last;
}, codeValid:function(sId){
this.id = $.trim(sId);//id去空格
this.sBirthday= this.id.substr(6, 4) + "-" + Number(this.id.substr(10, 2)) + "-" + Number(this.id.substr(12, 2));//出生年月
this.sSex = this.aCity[parseInt(this.id.substr(0, 2))] + "," + this.sBirthday + "," + (this.id.substr(16, 1) % 2 ? "男" : "女");
this.sDate = new Date(this.sBirthday.replace(/-/g, "/"));
this.sAddress = this.id.replace(/x$/i, "a");//地址 var iSum;
if(!sId || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/i.test(sId)){ this.tip = "你输入的身份证长度或格式错误!";
this.pass = false; }else if(this.aCity[parseInt(this.sAddress.substr(0, 2))] == null) { this.tip = "你的身份证地区非法!";
this.pass = false; }else if(this.sBirthday != (this.sDate.getFullYear() + "-" + (this.sDate.getMonth() + 1) + "-" + this.sDate.getDate())) {
this.tip = "身份证上的出生日期非法!";
this.pass = false;
}else{ var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校验位
var last = this.computeCheckBit(sId);
console.log('==='+last);
if(sId.substr(-1,1)!=last){
console.log(1);
this.tip = "你输入的身份证号校验位错误!";
this.pass = false;
} } if (!this.pass){
//alert(this.tip);
} return this.pass;
}
}
3.new对象进行验证
var idObj = new IdentityCodeValid(id);
console.log(idObj);//打印返回的信息即知是否通过验证