<!DOCTYPE html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1"><title></title></head><body>身份证号:<input type="text" name="tidentity_border" id="tidentity_border" onChange="GetBir_and_Sex(this.id,'born_border','age_boorder','gender_border')" /><br>性别:<select name="gender_border" id="gender_border"><option value="">=选择性别=</option><option value="男">男</option><option value="女">女</option></select><br>出生年月<input type="text" name="born_border" id="born_border" /><br>年龄:<input type="text" name="age_boorder" id="age_boorder" /><script>// 根据身份证号获取 生日、 年龄、 性别//生日function GetBir_and_Sex(IDNu, born_border, ages, sex) {var IDNu = document.getElementById(IDNu);var re =/^\d{6}(((19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}([0-9]|x|X))|(\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{3}))$/;if (!re.test(IDNu.value)) {alert("身份证号码有误,请重新填写!");IDNu.focus();//location.replace("");return false;}var str = IDNu.value;var born_border = document.getElementById(born_border);try {if (typeof(eval(born_border)) == "undefined") {//alert('参数born_border为空');} else {born_border.value = str.substring(6, 10) + "-" + str.substring(10, 12) + "-" + str.substring(12, 14);}} catch (e) {//alert('参数born_border错误');}//年龄 var ages = document.getElementById(ages);var myDate = new Date();var month = myDate.getMonth() + 1;var day = myDate.getDate();var age = myDate.getFullYear() - str.substring(6, 10) - 1;try {if (typeof(eval(ages)) == "undefined") {//alert('参数born_border为空');} else {if (str.substring(10, 12) < month || str.substring(10, 12) == month && str.substring(12, 14) <= day) {age++;}ages.value = age;}} catch (e) {//alert('参数born_border错误');}//性别 var sec = str.substring(16, 17);var sex = document.getElementById(sex);if (sec % 2 == 0) {var sew = "女";} else {var sew = "男";}sex.value = sew;for (i = 0; i < sex.options.length; i++)if (sex.options[i].value == sew) {sex.options[i].selected = true;}}</script></body></html>
当我们输入身份证号时,可以通过身份证号的前6位数字来获取到出生年月信息,通过身份证号的第15位数字来获取性别信息。年龄可以通过当前时间减去出生年月来计算得出。具体实现过程可以通过解析身份证号来实现,比如使用正则表达式来获取身份证号的各个部分信息。