Jquery身份证号校验

码农公社  210.net.cn   210= 1024  10月24日一个重要的节日--码农(程序员)节

身份证号码验证规则 输入身份证号码: 提交 代码如下:

<!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="/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>


评论