Node.js通过身份证号验证年龄、出生日期与性别方法示例


Posted in Javascript onMarch 09, 2017

前言

大家如果想要知道自己的年龄,出生日期和性别,或者是别人的,给我个身份证号,我就可以知道,其实很简单的,看下面代码。

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() ); 
  }   
 });

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Javascript 相关文章推荐
js中parseFloat(参数1,参数2)定义和用法及注意事项
Jan 27 Javascript
js实现点击按钮后给Div图层设置随机背景颜色的方法
May 06 Javascript
实例代码详解jquery.slides.js
Nov 16 Javascript
js实现把图片的绝对路径转为base64字符串、blob对象再上传
Dec 29 Javascript
js事件on动态绑定数据,绑定多个事件的方法
Sep 15 Javascript
vue中接口域名配置为全局变量的实现方法
Sep 20 Javascript
vue中使用axios post上传头像/图片并实时显示到页面的方法
Sep 27 Javascript
原生JS 实现的input输入时表格过滤操作示例
Aug 03 Javascript
JS数组方法join()用法实例分析
Jan 18 Javascript
JavaScript检测浏览器是否支持CSS变量代码实例
Apr 03 Javascript
Vue Object.defineProperty及ProxyVue实现双向数据绑定
Sep 02 Javascript
JavaScript大数相加相乘的实现方法实例
Oct 18 Javascript
基于jQuery实现一个marquee无缝滚动的插件
Mar 09 #Javascript
jQuery插件HighCharts绘制2D圆环图效果示例【附demo源码下载】
Mar 09 #Javascript
js实现显示手机号码效果
Mar 09 #Javascript
jQuery插件HighCharts绘制2D半圆环图效果示例【附demo源码下载】
Mar 09 #Javascript
javascript 秒表计时器实现代码
Mar 09 #Javascript
react实现pure render时bind(this)隐患需注意!
Mar 09 #Javascript
使用bootstrap-paginator.js 分页来进行ajax 异步分页请求示例
Mar 09 #Javascript
You might like
PHP面向对象程序设计之接口用法
2014/08/20 PHP
PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
2016/05/06 PHP
Mozilla中显示textarea中选择的文字
2006/09/07 Javascript
js 中 document.createEvent的用法
2010/08/29 Javascript
Javascript实现仿WebQQ界面的“浮云”兼容 IE7以上版本及FF
2011/04/27 Javascript
关于jQuery UI 使用心得及技巧
2012/10/10 Javascript
form表单中去掉默认的enter键提交并绑定js方法实现代码
2013/04/01 Javascript
js获得地址栏?问号后参数的方法
2013/08/08 Javascript
js保留小数点后几位的写法
2014/01/03 Javascript
javascript实现避免页面按钮重复提交
2015/01/08 Javascript
javascript的switch用法注意事项分析
2015/02/02 Javascript
TypeScript 中接口详解
2015/06/19 Javascript
knockoutjs动态加载外部的file作为component中的template数据源的实现方法
2016/09/01 Javascript
微信小程序 toast 详解及实例代码
2016/11/09 Javascript
使用Vue-Router 2实现路由功能实例详解
2017/11/14 Javascript
Angular利用trackBy提升性能的方法
2018/01/26 Javascript
layui实现数据分页功能
2019/07/27 Javascript
[01:20]辉夜杯背景故事宣传片《辉夜传说》
2015/12/25 DOTA
Python实现分割文件及合并文件的方法
2015/07/10 Python
12步入门Python中的decorator装饰器使用方法
2016/06/20 Python
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
python网络应用开发知识点浅析
2019/05/28 Python
Django项目主urls导入应用中views的红线问题解决
2019/08/10 Python
Python lambda表达式filter、map、reduce函数用法解析
2019/09/11 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
2019/12/27 Python
pytorch对梯度进行可视化进行梯度检查教程
2020/02/04 Python
Python 随机生成测试数据的模块:faker基本使用方法详解
2020/04/09 Python
Python 如何在字符串中插入变量
2020/08/01 Python
保送生自荐信范文
2013/10/06 职场文书
开办饭店创业计划书
2013/12/28 职场文书
资助贫困学生倡议书
2014/05/16 职场文书
2014年最新党员对照检查材料汇总
2014/09/15 职场文书
警告通知
2015/04/25 职场文书
驻村工作简报
2015/07/20 职场文书
详解Vue router路由
2021/11/20 Vue.js
高性能跳频抗干扰宽带自组网电台
2022/02/18 无线电