身份证号校验---------前端
程序员文章站
2022-06-16 22:10:06
身份证号码验证规则 输入身份证号码:提交 代码如下:
身份证号码验证规则
输入身份证号码:
代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en""http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>身份证号码验证规则</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
$(function(){
$('.btn').click(function(){
let orginnumber =$('.number').val();
let partn = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9xx]$/;//先用正则判断是否为正确的格式
if (!partn.exec(orginnumber))
{
alert('请检查你输入的身份证个式是否正确!');
}else{
let number = [...orginnumber]; //es6语法,利用...将变量的值转成数组。
let ration= new array("7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2");//系数
let ration22= new array("1","0","x","9","8","7","6","5","4","3","2");
let sum=0;
console.log(ration);
for(let i=0;i<number.length-1;i++){
sum=number[i]*ration[i]+sum;
console.log(i);
}
let mod = sum%11;
if(ration22[mod]==number[17]){
alert('正确');
}else{
alert('不正确');
}
}
})
})
</script>
</head>
<body>
<div>
<lable for="input">输入身份证号码:</lable><input type="text" class="number"/><button class="btn">提交</button>
</div>
</body>
</html>
上一篇: Python入门之面向对象和类
下一篇: 深入了解Vue组件七种通信方式