身份證號碼驗證規則

/**
* 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", "國外");
    }
};
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容