亲宝软件园·资讯

展开

jQuery实现根据身份证号获取生日、年龄、性别等信息的方法

The_road_of_ordinary 人气:0

本文实例讲述了jQuery实现根据SFZ号获取生日、年龄、性别等信息的方法。分享给大家供大家参考,具体如下:

jquery根据SFZ号获取生日、年龄、性别

1.html

<input type="text" />
<input type="button" onclick="getCode()" value="查询"/>

2.js

var getCode = function () {
 var ele = $("input").val();
 var birth = ele.substring(6, 10) + "-" + ele.substring(10, 12) + "-" + ele.substring(12, 14);
 console.log(birth);
 var sex = "";
 if (parseInt(ele.substr(16, 1)) % 2 == 1) {
  sex = "男";
 } else {
  sex = "女";
 }
 console.log(sex);
 //获取年龄
 var myDate = new Date();
 var month = myDate.getMonth() + 1;
 var day = myDate.getDate();
 var age = myDate.getFullYear() - ele.substring(6, 10) - 1;
 if (ele.substring(10, 12) < month || ele.substring(10, 12) == month && ele.substring(12, 14) <= day) {
  age++;
 }
 console.log(age);
};

效果:

完整示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery根据SFZ号获取生日、年龄、性别</title>
</head>
<body>
<input type="text" />
<input type="button" onclick="getCode()" value="查询"/>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
var getCode = function () {
 var ele = $("input").val();
 var birth = ele.substring(6, 10) + "-" + ele.substring(10, 12) + "-" + ele.substring(12, 14);
 console.log(birth);
 var sex = "";
 if (parseInt(ele.substr(16, 1)) % 2 == 1) {
  sex = "男";
 } else {
  sex = "女";
 }
 console.log(sex);
 //获取年龄
 var myDate = new Date();
 var month = myDate.getMonth() + 1;
 var day = myDate.getDate();
 var age = myDate.getFullYear() - ele.substring(6, 10) - 1;
 if (ele.substring(10, 12) < month || ele.substring(10, 12) == month && ele.substring(12, 14) <= day) {
  age++;
 }
 console.log(age);
};
</script>
</body>
</html>

另外,本站在线工具小程序上也有一款功能更加强大的SFZ信息获取工具,感兴趣的朋友可以扫描如下小程序码查看:

希望本文所述对大家jQuery程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论