身份證號(hào)碼驗(yàn)證規(guī)則

/**
* 1. 身份證由十七位數(shù)字本體碼和一位校驗(yàn)碼組成。排列順序從左至右依次為:六位數(shù)字地址碼,八位數(shù)字出生日期碼,三位數(shù)字順序碼和一位數(shù)字校驗(yàn)碼。
* 2. 地址碼表示省市縣編碼
* 3. 八位出生日期  如:19880321
* 4. 順序碼表示在同一地址碼所標(biāo)識(shí)的區(qū)域范圍內(nèi),對(duì)同年、同月、同日出生的人編定的順序號(hào),順序碼的奇數(shù)分配給男性,偶數(shù)分配給女性。
* 5. 校驗(yàn)碼
*    15 位身份證年份用 2 位表示(年份的后兩位,前面加上 18 或 19 表示 1800-1999 年出生的人。 2000 年之后出生的都是 18 位)。15 位身份證無(wú)校驗(yàn)碼,正好少 3 位。
*    18 位身份證最后一位是校驗(yàn)碼
*    校驗(yàn)碼計(jì)算規(guī)則:Sum(ai×Wi) (mod 11)
*    i----表示號(hào)碼字符從右至左包括校驗(yàn)碼在內(nèi)的位置序號(hào);
*    ai---表示第i位置上的號(hào)碼字符值;
*    Wi---示第i位置上的加權(quán)因子,其數(shù)值依據(jù)公式Wi=2^(n-1)(mod 11)計(jì)算得出。
*     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
*
*    舉個(gè)例子:53010219200508011x
*    求前 17 位加權(quán)和: (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
*    對(duì)11取余: 189%11 = 2
*    查詢校驗(yàn)碼表: ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] 位置 2 對(duì)應(yīng)的字符是 X, 所以最后一個(gè)位是 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))) {
        // 這里只檢查省份代碼,更嚴(yán)格點(diǎn)可以連區(qū)縣也檢查了
        return false;
    } else {
        if (code.length() == 18) {
            //加權(quán)因子
            int[] factor = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            //校驗(yàn)位
            char[] parity = new char[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

            // 計(jì)算加權(quán)和
            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", "內(nèi)蒙古");
        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", "臺(tái)灣");
        this.put("81", "香港");
        this.put("82", "澳門");
        this.put("91", "國(guó)外");
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容