身份证前端验证
---在线测试
验证:
var checkRule = { //身份证 idcard : function(val){ var regExp = /(^\d{15}$)|(^\d{17}(\d|X)$)/; //字数限制 if(!regExp.test(val)){ return 1; }//省份判断 var provincearr = ['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 province = val.substring(0,2); var provinceFlag = false; $.each(provincearr,function(i,city){ if(city == province){ provinceFlag = true; } }); if(!provinceFlag){ return 1; } //出生日期判断 var date = new Date(); var cyear = date.getFullYear(); var cmon = date.getMonth()+1; var cday = date.getDate(); var year = val.substring(6,10); var mon = val.substring(10,12); var day = val.substring(12,14); if(day < 0 || day > 31){ return 1; } if(mon < 0 || mon > 12){ return 1; } if((cyear - year) < 3 || (cyear - year) > 120){ return 1; } if((cyear - year) < 18){ return 2; } if((cyear - year) == 18){ if (cmon < mon) { return 2; } if ((cmon == mon)&&(cday<day)) { return 2; } } //15位的不需要验证了 if (val.length == 15){ return 0; } //奇偶验证 var i=0, last=0; var keys = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var vals = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; for(i = 0; i < 17; i++){ last += val.substr(i, 1) * keys[i]; } var lastchar = vals[last % 11]; if(lastchar != val.substr(-1)){ return 1; } return 0; }, //姓名 uname : function(val){ var regExp = /^[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{1,5})*$/; return regExp.test(val); } };</pre>
调用:
switch (checkRule.idcard(idcard)){ case 0: errorEle.html(""); $icon.removeClass('idcard-rightIcon').removeClass('idcard-wrongIcon').addClass("idcard-rightIcon"); return true; case 1: errorEle.html("请填写正确的身份证号码");break; case 2: errorEle.html("请输入成年人身份证号码");break; }
标题:身份证前端验证
作者:hugh0524
地址:https://blog.uproject.cn/articles/2016/03/24/1458816465171.html
0 0