身份证号码验证规则

/**
* 1. 身份证由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
* 2. 地址码表示省市县编码
* 3. 八位出生日期  如:19880321
* 4. 顺序码表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。
* 5. 校验码
*    15 位身份证年份用 2 位表示(年份的后两位,前面加上 18 或 19 表示 1800-1999 年出生的人。 2000 年之后出生的都是 18 位)。15 位身份证无校验码,正好少 3 位。
*    18 位身份证最后一位是校验码
*    校验码计算规则:Sum(ai×Wi) (mod 11)
*    i----表示号码字符从右至左包括校验码在内的位置序号;
*    ai---表示第i位置上的号码字符值;
*    Wi---示第i位置上的加权因子,其数值依据公式Wi=2^(n-1)(mod 11)计算得出。
*     i 18 17 16 15 14 13 12 11 10 9 8 7 6  5 4 3 2 1
*     Wi 7 9 10  5  8  4  2  1  6  3 7 9 10 5 8 4 2 1
*
*    举个例子:53010219200508011x
*    求前 17 位加权和: (5*7)+(3*9)+(0*10)+(1*5)+(0*8)+(2*4)+(1*2)+(9*1)+(2*6)+(0*3)+(0*7)+(5*9)+(0*10)+(8*5)+(0*8)+(1*4)+(1*2) = 189
*    对11取余: 189%11 = 2
*    查询校验码表: ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] 位置 2 对应的字符是 X, 所以最后一个位是 X。
*/
public static boolean checkIdCode(String code) {
    if (code == null) {
        return false;
    }

    if (!code.matches("^\\d{6}(18|19|20|21)?\\d{2}(0[1-9]|10|11|12)(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[X|x])$")) {
        return false;
    } else if (!cityMap.containsKey(code.substring(0, 2))) {
        // 这里只检查省份代码,更严格点可以连区县也检查了
        return false;
    } else {
        if (code.length() == 18) {
            //加权因子
            int[] factor = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            //校验位
            char[] parity = new char[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

            // 计算加权和
            int sum = 0;
            for (int i = 0; i < 17; i++) {
                sum += (code.charAt(i) - 48) * factor[i];
            }

            int parityIndex = sum % 11;
            if (parityIndex == 2) {
                // X x 都可以
                if (code.charAt(17) == 'X' || code.charAt(17) == 'x') {
                    return true;
                }
            } else {
                if (parity[parityIndex] == code.charAt(17)) {
                    return true;
                }
            }
        }
    }

    return false;
}

private static Map<String, String> cityMap = new HashMap<String, String>() {
    {
        this.put("11", "北京");
        this.put("12", "天津");
        this.put("13", "河北");
        this.put("14", "山西");
        this.put("15", "内蒙古");
        this.put("21", "辽宁");
        this.put("22", "吉林");
        this.put("23", "黑龙江");
        this.put("31", "上海");
        this.put("32", "江苏");
        this.put("33", "浙江");
        this.put("34", "安徽");
        this.put("35", "福建");
        this.put("36", "江西");
        this.put("37", "山东");
        this.put("41", "河南");
        this.put("42", "湖北");
        this.put("43", "湖南");
        this.put("44", "广东");
        this.put("45", "广西");
        this.put("46", "海南");
        this.put("50", "重庆");
        this.put("51", "四川");
        this.put("52", "贵州");
        this.put("53", "云南");
        this.put("54", "西藏");
        this.put("61", "陕西");
        this.put("62", "甘肃");
        this.put("63", "青海");
        this.put("64", "宁夏");
        this.put("65", "新疆");
        this.put("71", "台湾");
        this.put("81", "香港");
        this.put("82", "澳门");
        this.put("91", "国外");
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容