纯JS检测身份证合法性

原创
2016/07/09 16:31
阅读数 331
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>检测身份证合法性</title>
    <style type="text/css">
        .box {width: 300px;margin: 32px auto;}
        input {padding: 2px 4px;}
        #identity {width: 240px;}
        #result {color: #4D7ADA;}
        .muted {color: #999;}
    </style>
</head>
<body>
    <div class="box">
        <form id="form">
            <input type="text" id="identity" maxlength="18" pattern="\d{17}[0-9Xx]"
                title="18位身份证号码" placeholder="请填写身份证号"
                list="sample" />
            <datalist id="sample">
                <option value="380524198002300016"></option>
                <option value="340524198002300019"></option>
                <option value="340524197711111111"></option>
                <option value="34052419800101001X"></option>
            </datalist>
            <input type="submit" value="检测" />
        </form>
        <br><br><br>
        <span class="muted">检测结果:</span><span id="result"></span>
        <br><br><br>
        <span class="muted">原理:请查看页面源码,快捷键F12</span>
    </div>

<script type="text/javascript">
(function() {
    var City={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 arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

    function parseIdentity(id) {
        var result = {msg: ''};
        id = id.replace('x', 'X');
        if (!/^\d{17}[0-9X]$/.test(id)) {
            result.msg = '格式不正确';
            return result;
        }
        // 地区
        if (!City[id.slice(0,2)]) {
            result.msg = '非法地区';
            return result;
        }
        // 生日
        var birthday = [
            parseInt(id.slice(6,10), 10),
            parseInt(id.slice(10,12), 10),
            parseInt(id.slice(12,14), 10)
        ];
        var d = new Date();
        d.setFullYear(birthday[0], birthday[1] - 1, birthday[2]);
        if ((d.getFullYear() !== birthday[0]) || (
            d.getMonth() + 1 !== birthday[1]) || (
            d.getDate() !== birthday[2])) {
            result.msg = '非法生日';
            return result;
        }
        // 校验位
        var nTemp = 0, i = 0;
        for (; i < 17; i++) {
            nTemp += id.charAt(i) * arrInt[i];
        }
        var valnum = arrCh[nTemp % 11];
        if (valnum !== id.slice(-1)) {
            result.msg = '校验码不正确!应该为:' + valnum;
            return result;
        }
        result.birthday = birthday.join('-');
        result.district = City[id.slice(0,2)];
        result.sex = id.slice(16,17) % 2 ? "男" : "女";
        return result;
    }

    document.getElementById('form').onsubmit = function() {
        var id = document.getElementById('identity').value.trim();
        if (id) {
            var result = parseIdentity(id);
            var output = document.getElementById('result');
            if (result.msg) {
                output.textContent = result.msg;
            } else {
                output.textContent = result.district +
                    ', ' + result.birthday +
                    ', ' + result.sex;
            }
        }
        return false;
    };
})();
</script>
</body>
</html>

参考链接:

展开阅读全文
加载中
点击加入讨论🔥(1) 发布并加入讨论🔥
1 评论
8 收藏
0
分享
返回顶部
顶部