js 根据身份证获取出生日期、性别及年龄
第一步:身份证号格式校验
/** * 身份证号格式校验 */ function idValidator(obj) { var provinces={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 expression=/(^\d{15}$)|(^\d{17}(\d|X)$)/; var isViald=expression.test(obj.value); if (isViald) { isViald=provinces[obj.value.substr(0,2)]?true:false; } return isViald; }
第二步:通过身份证获取出生日期及性别
/**
* 通过身份证获取出生日期及性别
* @param idCard 15/18位身份证号码
* @return JSON对象
* sex:0-女、1-男;
* birthDay:yyyy-MM-dd
*/
function getBirthdayAndSex(idCard) {
var info = {};
var birth = (idCard.length === 18) ? idCard.slice(6, 14) : idCard.slice(6, 12);
// 18位:提取第17位数字;15位:提取最后一位数字
var order = (idCard.length == 18) ? idCard.slice(-2, -1) : idCard.slice(-1);
info.birthDay = (idCard.length === 18) ? ([birth.slice(0, 4), birth.slice(4, 6), birth.slice(-2)]).join('-')
: (['19' + birth.slice(0, 2), birth.slice(2, 4), birth.slice(-2)]).join('-');
// 余数为0代表女性,不为0代表男性
info.sex = (order % 2 === 0 ? 0 : 1);
return info;
}
第三步:改变出生日期及性别的值及只读状态
/** * 改变出生日期及性别的值及只读状态 */ function changeCardInfo(obj) { if ("" == obj.value && !document.getElementById('BIRTH_DAY2').disabled) return; // 格式校验结果 var result = idValidator(obj); if (result) { // 获取出生日期及性别 var cardInfo = getBirthdayAndSex(obj.value); var birthDay = cardInfo.birthDay; var sex = cardInfo.sex; document.getElementById('BIRTH_DAY2').disabled = true; document.getElementById('BIRTH_DAY2').value = birthDay; document.getElementById('BIRTH_DAY').value = birthDay; document.getElementById('male').disabled = true; document.getElementById('female').disabled = true; if (1 == sex) { document.getElementById('male').checked = true; document.getElementById('SEX').value = '男'; } else { document.getElementById('female').checked = true; document.getElementById('SEX').value = '女'; } } else {//重置 document.getElementById('BIRTH_DAY').value = ""; document.getElementById('BIRTH_DAY2').value = ""; document.getElementById('BIRTH_DAY2').disabled = false; document.getElementById('male').disabled = false; document.getElementById('female').disabled = false; document.getElementById('male').checked = true; document.getElementById('SEX').value = '男'; } }
HTML
<table border="0" cellpadding="1" cellspacing="0" align="center"> <tbody> <tr> <td>身份证号</td> <td> <input name="CARD_ID" class="TextBox" type="text" dataType="idcard" id="CARD_ID" errMessage="请输入正确的身份证号!" onblur="changeCardInfo(this);" /> </td> <td>性别</td> <td> <label> <input name="SEX1" type="radio" checked id="male" onchange="$('#SEX').val('男');" /> 男 </label> <label> <input name="SEX1" type="radio" id="female" onchange="$('#SEX').val('女');" /> 女 </label> <input name="SEX" type="hidden" id="SEX" value="男" /> </td> <td>出生日期</td> <td> <input class="TextBox" readonly id="BIRTH_DAY2" type="text" onclick="WdatePicker({maxDate:'%y-%M-%d'});" onblur="$('#BIRTH_DAY').val(this.value)" /> <input name="BIRTH_DAY" id="BIRTH_DAY" type="hidden" /> </td> </tr> </tbody> </table>
根据身份证号获取出生日期、性别及年龄
this.check =function(e){
var name = e.name;
var val = e.value;
var pat;
if(!val) {
return;
}else{
//检验身份证
pat = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
//console.log(pat.test(val));
if(pat.test(val)){
var birth = (val.length === 18) ? val.slice(6, 14) : val.slice(6, 12);
//计算出生日期
var birthDay = (val.length === 18) ? ([birth.slice(0, 4),
birth.slice(4, 6), birth.slice(-2)
]).join('-') : ([
'19' + birth.slice(0, 2), birth.slice(2, 4),
birth.slice(-2)
]).join('-');
//计算年龄
var birthyear = (val.length === 18) ? (birth.slice(0, 4)) :('19' + birth.slice(0, 2));
var birthmonth = (val.length === 18) ? (birth.slice(4, 6)) :('19' + birth.slice(2, 4));
var birthday = birth.slice(-2);
var now = new Date();
var nowyear = now.getFullYear();
var newmonth = now.getMonth() + 1;
var nowday = now.getDate();
var age = nowyear - birthyear;
if(newmonth < birthmonth || (newmonth == birthmonth && nowday < birthday)){
age--;
}
// 出生日期
$('#BIRTH_DAY').val(birthDay);
// 年龄
$('#AGE').val(age);
// 计算性别
var genderCode; // 性别代码
if (val.length == 18) { // 二代身份证号码长度为18位(第17位为性别代码)
genderCode = val.charAt(16);
} else { // 一代身份证号码长度为15位(第15位为性别代码)
genderCode = val.charAt(14);
}
// 两代身份证号码的性别代码都为男奇女偶
if (parseInt(genderCode) % 2 == 0) {// 女
$get('SEX').selectedIndex = 2;
} else {// 男
$get('SEX').selectedIndex = 1;
}
// 聚焦联系方式
$get('PHONE').focus();
}else{
Dialog.Alert("消息提示", "身份证号格式错误");
$('#CARD_ID').val('');
};
}
};
2023年5月11日10:29:17
根据出生日期,计算周岁
var DateUtils = {};
//JS日期系列:根据出生日期 得到周岁年龄
//参数strBirthday已经是正确格式的2007-02-09这样的日期字符串
DateUtils.getAge = function(strBirthday) {
var returnAge;
var strBirthdayArr = strBirthday.split("-");
var birthYear = strBirthdayArr[0];
var birthMonth = strBirthdayArr[1];
var birthDay = strBirthdayArr[2];
d = new Date();
var nowYear = d.getYear();
var nowMonth = d.getMonth() + 1;
var nowDay = d.getDate();
if (nowYear == birthYear) {
returnAge = 0;//同年 则为0岁
} else {
var ageDiff = nowYear - birthYear; //年之差
if (ageDiff > 0) {
if (nowMonth == birthMonth) {
var dayDiff = nowDay - birthDay;//日之差
if (dayDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
} else {
var monthDiff = nowMonth - birthMonth;//月之差
if (monthDiff < 0) {
returnAge = ageDiff - 1;
} else {
returnAge = ageDiff;
}
}
} else {
returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
}
}
return returnAge;//返回周岁年龄
}
写在最后
哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!
相关推荐:
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/6932566.html