Use JS to verify ID number

1. coding rules for Chinese resident ID card number

The first and second digits indicate provinces (autonomous regions, municipalities directly under the central government, and special administrative regions), and the region code.
The third and fourth digits indicate the city (the summary code of prefecture level city, autonomous prefecture, league and municipal districts and counties directly under the central government). Where, 01-20 and 51-70 refer to provinces and municipalities directly under the central government; 21-50 refers to region (autonomous prefecture, League).
The fifth and sixth digits indicate counties (municipal districts, county-level cities and banners). 01-18 refers to county-level cities under the jurisdiction of municipal districts or regions (autonomous prefectures, leagues); 21-80 indicates county (flag); 81-99 refers to county-level cities directly under the central government.
The seventh and fourteenth digits indicate the date of birth (the left side of the single digit month is filled with 0). The year is represented by four digits, and there is no separator between year, month and day. For example, May 11, 1981 is represented by 19810511.
The fifteenth and seventeenth digits indicate the sequence code. The sequence number assigned to persons born in the same region, year, month and day. The seventeenth odd number is given to men and the even number to women.
The eighteenth digit indicates the check code. The check code as the tail number is calculated by the number preparation unit according to the unified formula. If the check code has the number 10, it is replaced by X. for details, please refer to the calculation method below.
The first generation ID card number is 15 digits. The year is represented by two digits, and there is no verification code.
For details of the first six digits, please refer to Region code of province, city and county
X is a Roman character representing the number 10. Roman characters (1-12): I, II, III, IV, V, VI, VII, VIII, IX, x, Xi, XII... For details, please refer to Roman characters.

2. verification code algorithm of Chinese resident ID card

The steps are as follows:

  1. Multiply the 17 digits in front of the ID card number by different coefficients. The coefficients from the first to the seventeenth are 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2 respectively.
  2. Add the result of multiplying these 17 digits by the coefficient.
  3. Add it up and divide by 11 to get the remainder.
  4. The remainder can only contain 11 numbers: 0-1-2-3-4-5-6-7-8-9-10. The last corresponding ID card number is 1-0-X-9-8-7-6-5-4-3-2.
  5. The above calculation shows that if the remainder is 3, the verification code of the 18th bit is 9. If the remainder is 2, the corresponding check code is x, and X is actually the Roman numeral 10.

For example, the ID card number of a man is [53010219200508011x]. Let's see if this ID card is legal. First, we get that the product sum of the first 17 digits [(57) + (39) + (010) + (15) + (08) + (24) + (12) + (91) + (26) + (03) + (07) + (59) + (010) + (85) + (08) + (14) + (1*2)] is 189, and then we divide 189 by 11 to get 189/11= 17---2, that is, the remainder is 2. Finally, through the corresponding rules, we can know that the check code corresponding to remainder 2 is X. Therefore, it can be determined that this is a correct ID number.

3. JS implementation

Here is the implementation:

/**
 * Support 15 bit first-generation ID card and 18 Bit second-generation ID card
 * @param idCard Province certificate No
 * @returns {boolean} Return true if it is legal, otherwise false
 */
function validateIdCard(idCard) {
    var vcity = {
        11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia",
        21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu",
        33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan",
        42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing",
        51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu",
        63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "abroad"
    };
    //The ID card number has 15 or 18 digits, all of which are digits. The first 17 digits of the 18 digits are digits, and the last digit is the check digit, which may be digits or character X
    var reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/;
    if (!reg.test(idCard)) {
        return false;
    }
    //Check Province
    var provinceCode = idCard.substring(0, 2);
    if (vcity[provinceCode] == undefined)
        return false;
    //Detection of inspection bit
    return checkParity(idCard) != false;
}

/**
 * Check bit detection
 * @param card
 * @returns {boolean}
 */
function checkParity(card) {
    //15 bit to 18 Bit
    card = changeFifteenToEighteen(card);
    var len = card.length;
    if (len == 18) {
        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'];
        var sum = 0, i;
        for (i = 0; i < 17; i++) {
            sum += card.charAt(i) * arrInt[i];
        }
        var checkCode = arrCh[sum % 11];
        return checkCode == card.substring(17).toLocaleUpperCase();
    }
    return false;
}

function changeFifteenToEighteen(card) {
    if (card.length == 15) {
        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'];
        var sum = 0, i;
        card = card.substring(0, 6) + '19' + card.substring(6);
        for (i = 0; i < 17; i++) {
            sum += card.charAt(i) * arrInt[i];
        }
        var checkCode = arrCh[sum % 11];
        return card + checkCode;
    }
    return card;
}

Tags: Algorithm Javascript Front-end

Posted by iijb on Thu, 02 Jun 2022 23:24:41 +0530