HTML身份证号校验及根据身份证号获取出生日期/性别

一、HTML的方法都写在js里

       若HTML、JS、CSS在一个HTML文件中,在HTML中使用HTML需要借助<scirpt>标签,使用<script>标签的方式有两种:

        一种是直接在<script>标签中嵌入JavaScript代码(本文使用该方法)

       另一种是用<script>的src属性引入外部JavaScript文件

参考[1]:在HTML中使用JavaScript 

二、代码示例:

1、验证身份证号的合法性:

// 验证身份证号的合法性
    function checkIdNo(cid){ 
      var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子
      var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码
      if(/^\d{17}\d|x$/i.test(cid)){
        var sum = 0, idx;
        for(var i = 0; i < cid.length - 1; i++){
          // 对前17位数字与权值乘积求和
          sum += parseInt(cid.substr(i, 1), 10) * arrExp[i];
        }
        // 计算模(固定算法)
        idx = sum % 11;
        // 检验第18为是否与校验码相等
        return arrValid[idx] == cid.substr(17, 1).toUpperCase();
      }else{
        return false; 
      } 
    }

2、根据身份证号获取出生日期:

//获取出生日期
function getBirthFromCId(cid) {
	var birth = "";  
	if(cid != null && cid != ""){
		if(cid.length == 15){ 
			birth = "19"+cid.substr(6,6);  
		} else if(cid.length == 18){  
			birth = cid.substr(6,8);  
		}   
			birth = birth.replace(/(.{4})(.{2})/,"$1-$2-");  
		}   
	return birth;  
}
			                

 获取后,userMap拿到出生日期,需要调用方法(数据库中身份证号列名:cIdNo)

// 前端封装数据Map获取出生日期
userMap.birth=getBirthFromCId(cIdNo); 

3、通过出生日期获取性别:

//通过出生日期获取性别
 function getGenderByIDNo(IDNo){
	 if(IDNo!=null && IDNo!=''&& IDNo.length==18){ 
		if (parseInt(IDNo.substr(16, 1)) % 2 == 1) {
			 return 1; // 男
		} else {
		     return 0;  // 女
		} 
	} 
}

4、所有代码:

<!DOCTYPE html>
<html>
	<head></head>
	<body>
		<div></div>
		<script>
			// 验证身份证号的合法性
			    function checkIdNo(cid){ 
			      var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子
			      var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码
			      if(/^\d{17}\d|x$/i.test(cid)){
			        var sum = 0, idx;
			        for(var i = 0; i < cid.length - 1; i++){
			          // 对前17位数字与权值乘积求和
			          sum += parseInt(cid.substr(i, 1), 10) * arrExp[i];
			        }
			        // 计算模(固定算法)
			        idx = sum % 11;
			        // 检验第18为是否与校验码相等
			        return arrValid[idx] == cid.substr(17, 1).toUpperCase();
			      }else{
			        return false; 
			      } 
			    }
			    //获取出生日期
			    function getBirthFromCId(cid) {
			      var birth = "";  
			      if(cid != null && cid != ""){
			        if(cid.length == 15){ 
			          birth = "19"+cid.substr(6,6);  
			        } else if(cid.length == 18){  
			          birth = cid.substr(6,8);  
			        }   
			        birth = birth.replace(/(.{4})(.{2})/,"$1-$2-");  
			      }   
			      return birth;  
			    }
			   //通过出生日期获取性别
			    function getGenderByIDNo(IDNo){
			      if(IDNo!=null && IDNo!=''&& IDNo.length==18){ 
			        if (parseInt(IDNo.substr(16, 1)) % 2 == 1) {
			          return 1; // 男
			        } else {
			          return 0;  // 女
			        } 
			      } 
			    } 
		</script>
	</body>
</html>

5、我项目中的blur事件(记给自己看的,也在script中)

 //对身份证号blur,自动修改页面上性别的点击事件
$(document).on("blur","#cIdNo",function(){  
      var idno = $(this).val();
      var gender = getGenderByIDNo(idno); //调用方法从身份证号获得性别
      if(gender==1){
        $('.img_man').click();  //调用性别为“男”的点击事件
      }else{
        $('.img_woman').click();//调用性别为“女”的点击事件
      } 
    })

$(document).on(‘click’,‘要选择的元素’,function(){}) 用于动态事件的绑定

$(this).val();获取某个元素节点的value值,相当于$(this).attr(“value”);


参考[2]:$(选择器).click()和$(document).on(‘click‘,‘要选择的元素‘,function(){})的用法与不同:

参考[3]:$(document).on和$('.className').on区别

参考[4]:jquery中$(document).on的一些不解

参考[5]:jquery $(this).attr $(this).val方法使用介绍

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
html+JavaScript架构 可以用在.asp .php .jsp的用户注册页面中 功能如下: 1.可以验证用户密码的安全级别并返回安全级别的等级 全部数字安全级别为低 数字+字母安全级别为中 数字+大小写字母安全级别为最高 并且密码不能为全角 或者特殊符号等 如果修改功能请修改 js下的passwordstrength.js 2.姓名验证 验证中国人的姓名 3.身份证验证根据用户提供的身份证信判断出省市,然后在判断....做到只能输入 正确的省份证信息才可以.... 判断身份证的方法比使用正则表达式更加准确详细请看 js中的 checkidcard.js 判断省份证的 js function checkIsChinese(str){ if (str.length < 2 || str.length > 15){ return "姓名长度不正确"; } var ret=true; for(var i=0;i<str.length;i++){ ret=ret && (str.charCodeAt(i)>=10000); } if (ret){ return "true"; } else{ return "请输入中文"; } } //www.jsphelp.com function parseIdCard(val) { var birthdayValue; var sexId; var sexText; if (15 == val.length) //15位身份证号码 { birthdayValue = val.charAt(6) + val.charAt(7); if (parseInt(birthdayValue) < 10) { birthdayValue = '20' + birthdayValue; } else { birthdayValue = '19' + birthdayValue; } birthdayValue = birthdayValue + '-' + val.charAt(8) + val.charAt(9) + '-' + val.charAt(10) + val.charAt(11); if (parseInt(val.charAt(14) / 2) * 2 != val.charAt(14)) { sexId = "1"; sexText = "男"; } else { sexId = "2"; sexText = "女"; } } if (18 == val.length) { //18位身份证号码 birthdayValue = val.charAt(6) + val.charAt(7) + val.charAt(8) + val.charAt(9) + '-' + val.charAt(10) + val.charAt(11) + '-' + val.charAt(12) + val.charAt(13); if (parseInt(val.charAt(16) / 2) * 2 != val.charAt(16)) { sexId = "1"; sexText = "男"; } else { sexId = "2"; sexText = "女"; } } //年龄 var dt1 = new Date(birthdayValue.replace("-", "/")); var dt2 = new Date(); var age = dt2.getFullYear() - dt1.getFullYear(); var m = dt2.getMonth() - dt1.getMonth(); if (m < 0) age--; return new Array(birthdayValue, sexId, sexText, age); } function checkIdcard(idcard){ idcard = idcard.toUpperCase(); var Errors=new Array("true","身份证号码位数不对!","身份证号出生日期超出范围或含有非法字符!","身份证号校验错误!","身份证号码中地区编码不正确!"); var area={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 idcard,Y,JYM; var S,M; var idcard_array = new Array(); idcard_array = idcard.split(""); if(idcard.length != 15 && idcard.length != 18) return Errors[1]; //if(idcard.length != 18) return Errors[1]; if (idcard.substr(0,6) == "000000" || idcard.substr(0,6) == "111111" || idcard.substr(0,6) == "222222" || idcard.substr(0,6) == "333333" || idcard.substr(0,6) == "444444" || idcard.substr(0,6) == "555555" || idcard.substr(0,6) == "666666" || idcard.substr(0,6) == "777777" || idcard.substr(0,6) == "888888" || idcard.substr(0,6) == "999999") return Errors[4]; if (idcard.substr(0,6) == "123456" || idcard.substr(0,6) == "234567" || idcard.substr(0,6) == "345678" || idcard.substr(0,6) == "456789" || idcard.substr(0,6) == "567890" || idcard.substr(0,6) == "012345" || idcard.substr(0,6) == "543210" || idcard.substr(0,6) == "432109" || idcard.substr(0,6) == "321098" || idcard.substr(0,6) == "210987" || idcard.substr(0,6) == "109876" || idcard.substr(0,6) == "098765" || idcard.substr(0,6) == "987654" || idcard.substr(0,6) == "876543" || idcard.substr(0,6) == "765432") return Errors[4]; if (idcard.substr(0,6) == "121212" || idcard.substr(0,6) == "131313" || idcard.substr(0,6) == "141414" || idcard.substr(0,6) == "151515" || idcard.substr(0,6) == "161616" || idcard.substr(0,6) == "171717" || idcard.substr(0,6) == "181818" || idcard.substr(0,6) == "191919" || idcard.substr(0,6) == "101010") return Errors[4]; if (idcard.substr(0,6) == "212121" || idcard.substr(0,6) == "232323" || idcard.substr(0,6) == "242424" || idcard.substr(0,6) == "252525" || idcard.substr(0,6) == "262626" || idcard.substr(0,6) == "272727" || idcard.substr(0,6) == "282828" || idcard.substr(0,6) == "292929" || idcard.substr(0,6) == "202020") return Errors[4]; if (idcard.substr(0,6) == "313131" || idcard.substr(0,6) == "323232" || idcard.substr(0,6) == "343434" || idcard.substr(0,6) == "353535" || idcard.substr(0,6) == "363636" || idcard.substr(0,6) == "373737" || idcard.substr(0,6) == "383838" || idcard.substr(0,6) == "393939" || idcard.substr(0,6) == "303030") return Errors[4]; if (idcard.substr(0,6) == "414141" || idcard.substr(0,6) == "424242" || idcard.substr(0,6) == "434343" || idcard.substr(0,6) == "454545" || idcard.substr(0,6) == "464646" || idcard.substr(0,6) == "474747" || idcard.substr(0,6) == "484848" || idcard.substr(0,6) == "494949" || idcard.substr(0,6) == "404040") return Errors[4]; if (idcard.substr(0,6) == "515151" || idcard.substr(0,6) == "525252" || idcard.substr(0,6) == "535353" || idcard.substr(0,6) == "545454" || idcard.substr(0,6) == "565656" || idcard.substr(0,6) == "575757" || idcard.substr(0,6) == "585858" || idcard.substr(0,6) == "595959" || idcard.substr(0,6) == "505050") return Errors[4]; if (idcard.substr(0,6) == "616161" || idcard.substr(0,6) == "626262" || idcard.substr(0,6) == "636363" || idcard.substr(0,6) == "646464" || idcard.substr(0,6) == "656565" || idcard.substr(0,6) == "676767" || idcard.substr(0,6) == "686868" || idcard.substr(0,6) == "696969" || idcard.substr(0,6) == "606060") return Errors[4]; if (idcard.substr(0,6) == "717171" || idcard.substr(0,6) == "727272" || idcard.substr(0,6) == "737373" || idcard.substr(0,6) == "747474" || idcard.substr(0,6) == "757575" || idcard.substr(0,6) == "767676" || idcard.substr(0,6) == "787878" || idcard.substr(0,6) == "797979" || idcard.substr(0,6) == "707070") return Errors[4]; if (idcard.substr(0,6) == "818181" || idcard.substr(0,6) == "828282" || idcard.substr(0,6) == "838383" || idcard.substr(0,6) == "848484" || idcard.substr(0,6) == "858585" || idcard.substr(0,6) == "868686" || idcard.substr(0,6) == "878787" || idcard.substr(0,6) == "898989" || idcard.substr(0,6) == "808080") return Errors[4]; if (idcard.substr(0,6) == "919191" || idcard.substr(0,6) == "929292" || idcard.substr(0,6) == "939393" || idcard.substr(0,6) == "949494" || idcard.substr(0,6) == "959595" || idcard.substr(0,6) == "969696" || idcard.substr(0,6) == "979797" || idcard.substr(0,6) == "989898" || idcard.substr(0,6) == "909090") return Errors[4]; if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4]; switch(idcard.length){ case 15: if ((parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){ ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性 } else{ ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性 } if(ereg.test(idcard)) return Errors[0]; else return Errors[2]; break; case 18: if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){ ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式 } else{ ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式 } if(ereg.test(idcard)){ S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ; Y = S % 11; M = "F"; JYM = "10X98765432"; M = JYM.substr(Y,1); if(M == idcard_array[17]) return Errors[0]; else return Errors[3]; } else return Errors[2]; break; default: return Errors[1]; break; } } function checkTwID(id){ //建立字母分数数组(A~Z) var city = new Array( 1,10,19,28,37,46,55,64,39,73,82, 2,11, 20,48,29,38,47,56,65,74,83,21, 3,12,30 ) id = id.toUpperCase(); // 使用「正规表达式」检验格式 if (id.search(/^[A-Z](1|2)\d{8}$/i) == -1) { return "身份证号码格式不对!"; } else { //将字符串分割为数组(IE必需这么做才不会出错) id = id.split(''); //计算总分 var total = city[id[0].charCodeAt(0)-65]; for(var i=1; i<=8; i++){ total += eval(id[i]) * (9 - i); } //补上检查码(最后一码) total += eval(id[9]); //检查比对码(余数应为0); if (total%10 == 0) { return "true"; } else { return "身份证号码格式不对!"; } } }
可以使用正则表达式对输入的身份证号进行校验,然后根据身份证号的规则提取出生日期、年龄、性别等信息。 以下是一个示例代码: ```javascript // 校验身份证号 function validateIdCard(idCard) { var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; return reg.test(idCard); } // 获取出生日期 function getBirthDate(idCard) { var year, month, day; if (idCard.length == 15) { year = "19" + idCard.substring(6, 8); month = idCard.substring(8, 10); day = idCard.substring(10, 12); } else if (idCard.length == 18) { year = idCard.substring(6, 10); month = idCard.substring(10, 12); day = idCard.substring(12, 14); } return year + "-" + month + "-" + day; } // 获取年龄 function getAge(idCard) { var birthDate = getBirthDate(idCard); var age = new Date().getFullYear() - parseInt(birthDate.substring(0, 4)); if (new Date(birthDate).getMonth() > new Date().getMonth() || (new Date(birthDate).getMonth() == new Date().getMonth() && new Date(birthDate).getDate() > new Date().getDate())) { age--; } return age; } // 获取性别 function getGender(idCard) { var gender; if (idCard.length == 15) { gender = parseInt(idCard.charAt(14)) % 2 == 1 ? "男" : "女"; } else if (idCard.length == 18) { gender = parseInt(idCard.charAt(16)) % 2 == 1 ? "男" : "女"; } return gender; } // 在输入框输入时调用 function onInput(e) { var idCard = e.detail.value; if (validateIdCard(idCard)) { var birthDate = getBirthDate(idCard); var age = getAge(idCard); var gender = getGender(idCard); console.log("出生日期:" + birthDate + " 年龄:" + age + " 性别:" + gender); // TODO: 更新页面显示出生日期、年龄、性别等信息 } else { console.log("身份证号格式不正确"); // TODO: 更新页面提示身份证号格式不正确 } } ``` 在 `onInput` 函数中,首先校验输入的身份证号是否合法,如果合法则使用 `getBirthDate`、`getAge` 和 `getGender` 函数分别获取出生日期、年龄和性别,然后更新页面显示这些信息。如果身份证号格式不正确,则提示用户输入格式不正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值