Node.js通过身份证号验证年龄、出生日期与性别方法示例
程序员文章站
2023-12-13 18:34:16
前言
大家如果想要知道自己的年龄,出生日期和性别,或者是别人的,给我个身份证号,我就可以知道,其实很简单的,看下面代码。
node.js实现
static v...
前言
大家如果想要知道自己的年龄,出生日期和性别,或者是别人的,给我个身份证号,我就可以知道,其实很简单的,看下面代码。
node.js实现
static validateidnumbertoageyear(str){ let date = new date(); let currentyear = date.getfullyear(); let currentmonth = date.getmonth() + 1; let currentdate = date.getdate(); let idxsexstart = str.length == 18 ? 16 : 14; let birthyearspan = str.length == 18 ? 4 : 2; let year; let month; let day; let sex; let birthday; let age; //性别 let idxsex = 1 - str.substr(idxsexstart, 1) % 2; sex = idxsex == '1' ? '女' : '男'; //生日 year = (birthyearspan == 2 ? '19' : '') + str.substr(6, birthyearspan); month = str.substr(6 + birthyearspan, 2); day = str.substr(8 + birthyearspan, 2); birthday = year + '-' + month + '-' + day; //年龄 let monthfloor = (currentmonth < parseint(month,10) || (currentmonth == parseint(month,10) && currentdate < parseint(day,10))) ? 1 : 0; age = currentyear - parseint(year,10) - monthfloor; // console.log("我的出生日期是"+year+"年"+month+"月"+day+"日"+",今年"+age+"岁了"+",性别是"+sex); if(age >= 18){ return true; } return false; }
我这里只是做了一个年龄的判断。
利用js也可以实现
1. 自定义js类如下:
// 构造函数,变量为15位或者18位的身份证号码 function clsidcard(cardno) { this.valid = false; this.id15 = ''; this.id18 = ''; this.local = ''; if (cardno != null) this.setcardno(cardno); } // 设置身份证号码,15位或者18位 clsidcard.prototype.setcardno = function(cardno) { this.id15 = ''; this.id18 = ''; this.local = ''; cardno = cardno.replace(" ", ""); var strcardno; if (cardno.length == 18) { pattern = /^\d{17}(\d|x|x)$/; if (pattern.exec(cardno) == null) return; strcardno = cardno.touppercase(); } else { pattern = /^\d{15}$/; if (pattern.exec(cardno) == null) return; strcardno = cardno.substr(0, 6) + '19' + cardno.substr(6, 9) strcardno += this.getvcode(strcardno); } this.valid = this.checkvalid(strcardno); } // 校验身份证有效性 clsidcard.prototype.isvalid = function() { return this.valid; } // 返回生日字符串,格式如下,1981-10-10 clsidcard.prototype.getbirthdate = function() { var birthdate = ''; if (this.valid) birthdate = this.getbirthyear() + '-' + this.getbirthmonth() + '-' + this.getbirthday(); return birthdate; } // 返回生日中的年,格式如下,1981 clsidcard.prototype.getbirthyear = function() { var birthyear = ''; if (this.valid) birthyear = this.id18.substr(6, 4); return birthyear; } // 返回生日中的月,格式如下,10 clsidcard.prototype.getbirthmonth = function() { var birthmonth = ''; if (this.valid) birthmonth = this.id18.substr(10, 2); if (birthmonth.charat(0) == '0') birthmonth = birthmonth.charat(1); return birthmonth; } // 返回生日中的日,格式如下,10 clsidcard.prototype.getbirthday = function() { var birthday = ''; if (this.valid) birthday = this.id18.substr(12, 2); return birthday; } // 返回性别,1:男,0:女 clsidcard.prototype.getsex = function() { var sex = ''; if (this.valid) sex = this.id18.charat(16) % 2; return sex; } // 返回15位身份证号码 clsidcard.prototype.get15 = function() { var id15 = ''; if (this.valid) id15 = this.id15; return id15; } // 返回18位身份证号码 clsidcard.prototype.get18 = function() { var id18 = ''; if (this.valid) id18 = this.id18; return id18; } // 返回所在省,例如:上海市、浙江省 clsidcard.prototype.getlocal = function() { var local = ''; if (this.valid) local = this.local; return local; } clsidcard.prototype.getvcode = function(cardno17) { var wi = new array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); var ai = new array('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2'); var cardnosum = 0; for (var i = 0; i < cardno17.length; i++) cardnosum += cardno17.charat(i) * wi[i]; var seq = cardnosum % 11; return ai[seq]; } clsidcard.prototype.checkvalid = function(cardno18) { if (this.getvcode(cardno18.substr(0, 17)) != cardno18.charat(17)) return false; if (!this.isdate(cardno18.substr(6, 8))) return false; var 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 : "国外" }; if (acity[parseint(cardno18.substr(0, 2))] == null) return false; this.id18 = cardno18; this.id15 = cardno18.substr(0, 6) + cardno18.substr(8, 9); this.local = acity[parseint(cardno18.substr(0, 2))]; return true; } clsidcard.prototype.isdate = function(strdate) { var r = strdate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/); if (r == null) return false; var d = new date(r[1], r[2] - 1, r[3]); return (d.getfullyear() == r[1] && (d.getmonth() + 1) == r[2] && d .getdate() == r[3]); }
2. 页面只需要new出对象,并传递数据验证,并可获得相关数据( 住址 | 出生日期 | 性别 )即可:
$("#cardno").blur(function(event){ var idcard = $(this).val(); var checkflag = new clsidcard(idcard); if( !checkflag.isvalid() ){ alert("身份证错误"); return false; }else{ alert( "出生于: " + checkflag.getbirthdate() +" 地区:" + checkflag.getlocal() +" sex:" + checkflag.getsex() ); } });
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。