欢迎关注得胜的git仓库

身份证验证思路及代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <pre>
1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ;
2、将这17位数字和系数相乘的结果相加;
3、用加出来和除以11,看余数是多少;
4、余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2;
5、通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的X。
例如:某男性的身份证号码是34052419800101001X。我们要看看这个身份证是不是合法的身份证。
首先:我们计算3*7+4*9+0*10+5*5+...+1*2,前17位的乘积和是189
然后:用189除以11得出的结果是商17余2
最后:通过对应规则就可以知道余数2对应的数字是x。所以,这是一个合格的
        </pre>
        
        <!--35 89 01 80 69 72 41 7-->
        <input type="text" value='34052419800101001X' />
        <button onclick="check()">校验</button>
        <script type="text/javascript">
            var oInput = document.getElementsByTagName("input")[0];
            var coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 系数数组
            var checkArr = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; // 校验数组
            var year, month, day;
            function check(){
                var val = oInput.value;
                if(val && val.length == 18){
                    var lastBitCheck = checkNum(sumIdCard(val.slice(0,17)), val.slice(-1)); // 末位校验
                    year = val.slice(6,10);
                    month = val.slice(10, 12);
                    day = val.slice(12, 14);
                    
                    var yearCheck = checkYear(year);
                    var monthCheck = checkMonth(month);
                    var dayCheck = checkDay(year, month, day);
                    switch(false){
                        case lastBitCheck: console.log("末位校验失败"); break;
                        case yearCheck: console.log("出生年份校验失败"); break;
                        case monthCheck: console.log("出生月份校验失败"); break;
                        case dayCheck: console.log("出生日期校验失败"); break;
                        default:;
                    }
                    var res = lastBitCheck && yearCheck && monthCheck && dayCheck;
                    // console.log(year, month, day);
                    console.log("校验结果", res);
                    return res;
                }else{
                    // console.log("长度有误");
                    return false;
                }
            }
            
            function doubleNum (n){
                if(n === undefined || n === null){
                    return "";
                }
                n = String(n);
                if(n.length > 2){
                    return n
                }else{
                    n = "0" + n;
                    return n.slice(-2);
                }
            }
            
            function sumIdCard(str){ // 传入IMEI的前14位
                var res = 0, tempNum, tempStr;
                for(let i = 0, len = str.length; i < len; i++){
                    res += Number(str[i]) * coefficient[i];
                }
                // console.log(res);
                return res;
            }
            
            function checkNum(num, t){ // 校验位校验 num:sumIdCard的结果,t:idCard的最后一位校验位
                // console.log(num, t)
                var rightTestNum = checkArr[num % 11];
                console.log("正确的校验位", rightTestNum);
                if(String(rightTestNum).toUpperCase() == String(t).toUpperCase()){
                    return true;
                }else{
                    return false;
                }
            }
            
            function checkYear(year){
                year = Number(year);
                return (year >= 1800 && year <= new Date().getFullYear()) ? true : false;
            }
            
            function checkMonth(month){
                month = Number(month);
                return (month >= 1 && month <= 12) ? true : false;
            }
            
            function checkDay(year, month, day){
                year = Number(year);
                month = Number(month);
                day = Number(day);
                var d31 = [1, 3, 5, 7, 8, 10, 12]; // 31天的月份
                var d30 = [4, 6, 9, 11]; // 30天的月份
                if(d31.indexOf(month) != -1){
                    return (day >= 1 && day <= 31) ? true : false;
                }else if(d30.indexOf(month) != -1){
                    return (day >= 1 && day <= 30) ? true : false;
                }else if(month == 2){
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                        return (day >= 1 && day <= 29) ? true : false;
                    }else{
                        return (day >= 1 && day <= 28) ? true : false;
                    }
                }else{
                    // console.error(month,"月份错了(1-12)")
                    return false;
                }
            }
        </script>
    </body>
</html>

 

posted on 2019-01-14 20:03  web得胜  阅读(782)  评论(0编辑  收藏  举报

导航

感谢观看web得胜的博客,如果您觉得对你有帮助,请点赞支持一下哦~。发现问题请留言指正,谢谢!