文章分类 Classification
【网页编程】
身份证验证
稿件来源: 阳光企业网站管理系统 撰稿作者: 太阳光 发表日期: 2012-08-28 阅读次数: 651 查看权限: 游客查看
原网上摘录的验证不正确,故在此更正并做了15位与18位身份证兼容。关键验证了字符、出生日期与尾数。
javascript版本:
function isIdCard(arrIdCard){ //支持15/18位身份证验证 var IdCard=arrIdCard.toString().toUpperCase(); if(!/^\d{17}(\d|X)$/i.test(IdCard)&&!/^\d{15}$/i.test(IdCard)){ return false; } var birth; if(IdCard.length==18){ var sigma = 0; var a = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ); var w = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); for (var i = 0; i < 17; i++) { sigma += parseInt(IdCard.substring(i, i + 1)) * a[i]; } if (IdCard.substring(17) != w[sigma % 11]) { return false; } birth=IdCard.substring(6, 10)+"/"+IdCard.substring(10, 12)+"/"+IdCard.substring(12, 14); }else{ birth="19"+IdCard.substring(6, 8)+"/"+IdCard.substring(8, 10)+"/"+IdCard.substring(10, 12); } var date=new Date(birth); var getDate=date.getFullYear()+"/"+("0"+(date.getMonth()+1)).slice(-2)+"/"+("0"+date.getDate()).slice(-2); return getDate==birth; }
推荐另一种方法:
var checkCardId = function (card) { card=card+"";//比toString()好使; if(!/^((1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[X0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))$/i.test(card)){ return false; } var j = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var k = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; var n= 0,i; if (15==card.length){ //15位自动转18位 card = card.substring(0, 6) + '19' + card.substring(6); for (i = 0; i < 17; i++) { n += j[i]*card.substring(i, i+1); } card += k[n % 11]; } var b=card.substring(6, 10)+"/"+card.substring(10, 12)+"/"+card.substring(12, 14); var t=new Date(b); var d=t.getFullYear()+"/"+("0"+(t.getMonth()+1)).slice(-2)+"/"+("0"+t.getDate()).slice(-2); n=0; for (i = 0; i < 17; i++) { n += j[i] * card.substring(i,i+1); } var l=isNaN(card.slice(-1))?"X":card.slice(-1); return l==k[n % 11]&&d==b; };
java版本:
public static boolean isIdCard1(String arrIdCard){ //支持15/18位身份证验证 String idCard= arrIdCard.toUpperCase(); String test1 = "[0-9]{17}([0-9]|X)"; String test2 = "[0-9]{15}"; Pattern pattern1 = Pattern.compile(test1); Pattern pattern2 = Pattern.compile(test2); Matcher isNum1 = pattern1.matcher(idCard); Matcher isNum2 = pattern2.matcher(idCard); if(!isNum1.matches() && !isNum2.matches()){ return false; } String birth; if(idCard.length() == 18){ int sigma = 0; Integer[] a = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; String[] w = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; for (int i = 0; i < 17; i++) { int ai = Integer.parseInt(idCard.substring(i, i + 1)); int wi = a[i]; sigma += ai * wi; } int number = sigma % 11; String check_number = w[number]; if (!idCard.substring(17).equals(check_number)) { return false; } birth = idCard.substring(6, 10)+"-"+idCard.substring(10, 12)+"-"+idCard.substring(12, 14); }else{ birth="19"+idCard.substring(6, 8)+"-"+idCard.substring(8, 10)+"-"+idCard.substring(10, 12); } Date birDate = Utils.convert(birth); String birStr2 = (birDate.getYear()+1900)+"-"+(birDate.getMonth()+1)+"-"+birDate.getDate(); if(!birth.equals(birStr2)){ return false; }else { return true; } }
======================== 18位身份证检测
function checkCardId(card) { card = card.toString().toUpperCase(); var arr = card.match(/^\d{6}(\d{4})(\d{2})(\d{2})\d{3}([X0-9])$/); if(!arr) return false; var j = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var k = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; var t = new Date(arr[1] +"/"+ arr[2] +"/"+ arr[3]); t = t.getMonth() + 1 == arr[2] && t.getDate() == arr[3]; for (var i = 0,n = 0; i < 17; i++) { n += j[i] * card.substring(i,i+1); } return t && arr[4] == k[n % 11]; }
关键词: js,身份证验证 编辑时间: 2015-06-09 22:21:01
3
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
100%(2)
0%(0)
- 暂无评论
文章图片 article Pictrue
网友评论