Javascript Test ID card number 15-digit 18-digit

Source: Internet
Author: User
JS implements the last digit when the ID card number is 15 to 18 charactersAlgorithm

According to the provisions on the national identity number in the National Standard GB 11643-1999 of the People's Republic of China, the national identity number is a characteristic combination code consisting of a 17-digit digital ontology code and a digital verification code.

The order is from left to right: six-digit address code, eight-digit birthdate code, three-digit sequence code, and one-digit verification code.

1. Address CodeIndicates the administrative area of the county (city, flag, district) where the resident account of the encoding object is located.Code.

2. BirthdateIndicates the year, month, and day of the encoding object. The year is represented by four digits, and there is no separator between the year, month, and day.

3. Order codeIndicates the number of times that employees of the same year, month, and day are assigned within the region specified by the same address code. The odd number of the order code is given to men, and the even number is given to women.

4. Verification CodeThe verification code is calculated based on the first 17-digit code according to the ISO 7064: 1983.mod 11-2 verification code.

The following is an example of this calculation method.

The 15-digit ID card Code expands the birth year to four digits. Simply, it adds 19 digits, but this is not used by people born in 1900 (there are few such stars)

The subject code of a male national identity number is 34052419800101001. First, calculated according to formula (1:

SIGMA (AI x WI) (mod 11 ).......................................... (1)
Formula (1:
I ---- indicates the position serial number of The number character, including the verification code from the left;
AI ---- indicates the number character value in the I position;
Wi ---- indicates the weighting factor on the I position. The value is calculated based on the formula 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

AI 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 1 A1

Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

AI x wi 21 36 0 25 16 16 2 9 48 0 9 0 5 0 0 2 A1

Calculate according to formula (1:
SIGMA (AI x WI) = (21 + 36 + 0 + 25 + 16 + 16 + 2 + 9 + 48 + 0 + 0 + 9 + 0 + 5 + 0 + 0 + 2) = 189

189 usd11 = 17 + 2/11

SIGMA (AI x WI) (mod 11) = 2

Then, based on the calculation results, find the corresponding verification code from the following table, where X indicates that the calculation result is 10:

SIGMA (AI x WI) (mod 11) 0 1 2 3 4 5 6 7 8 9 10
Verification Code character value: AI 1 0x9 8 7 6 5 4 3 2
According to the above table, the verification code of the calculation result of 2 is found to be 34052419800101001x.

A [0] * 7 + A [1] * 9 + A [2] * 10 + A [3] * 5 + A [4] * 8 + A [5] * 4 + A [6] * 2 + A [7] * 1 + a [8] * 6 + A [9] * 3
+ A [10] * 7 + A [11] * 9 + A [12] * 10 + A [13] * 5 + A [14] * 8 + A [15] * 4 + A [16] * 2

 

The javascript code is as follows:

/**
* Verify if it is a number.
*/
Function isnumber (onum ){
If (! Onum) return false;
VaR STRP =/^ \ D + (\. \ D + )? $ /;
If (! STRP. Test (onum) return false;
Try {
If (parsefloat (onum )! = Onum) return false;
}
Catch (Ex)
{
Return false;
}
Return true;
}
/**
* Check whether the date is correct
*/
Function isvaliddate (Iy, Im, ID ){
If (Iy> 2200 | Iy <1900 |! Isnumber (Iy )){
Alert ("Enter ID card number, year" + Iy + "illegal! ");
Return false;
}
If (im> 12 | iM <= 0 |! Isnumber (IM )){
Alert ("Enter ID card number, Month" + IM + "illegal! ");
Return false;
}
If (ID> 31 | id <= 0 |! Isnumber (ID )){
Alert ("Enter ID card number, date" + ID + "illegal! ");
Return false;
}
Return true;
}
/**
* Function for determining the ID card number format
* A citizenship number is a feature combination code,
* The sorting order is from left to right: six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code.
* Usage onblur = "ischinaidcard (this. Value )"
*/
Function ischinaidcard (strno ){
Strno = strno. tostring ();
If (strno. Length = 15 ){
If (! Isvaliddate ("19" + strno. substr (6, 2), strno. substr (8, 2), strno. substr (10, 2 ))){
Return false;
}
} Else if (strno. Length = 18 ){
If (! Isvaliddate (strno. substr (6, 4), strno. substr (), strno. substr ))){
Return false;
}
} Else {
Alert ("the ID number must be 15 or 18 characters! ");
Return false;
}

If (strno. Length = 18 ){
VaR a, B, c
If (! Isnumber (strno. substr (0, 17 ))){
Alert ("Incorrect ID card number. The first 17 digits cannot contain English letters! ");
Return false;
}
A = parseint (strno. substr () * 7 + parseint (strno. substr () * 9 + parseint (strno. substr () * 10;
A = a + parseint (strno. substr () * 5 + parseint (strno. substr () * 8 + parseint (strno. substr () * 4;
A = a + parseint (strno. substr (6, 1) * 2 + parseint (strno. substr (7, 1) * 1 + parseint (strno. substr (8, 1) * 6;
A = a + parseint (strno. substr () * 3 + parseint (strno. substr () * 7 + parseint (strno. substr () * 9;
A = a + parseint (strno. substr () * 10 + parseint (strno. substr () * 5 + parseint (strno. substr () * 8;
A = a + parseint (strno. substr (15,1) * 4 + parseint (strno. substr (16,1) * 2;
B = A % 11;
If (B = 2) {// the last digit is the check bit
C = strno. substr (17,1). touppercase (); // convert to uppercase x
} Else {
C = parseint (strno. substr (17,1 ));
}
Switch (B ){
Case 0: If (C! = 1) {alert ("check digit of the ID card is incorrect: the last digit should be: 1"); Return false;} break;
Case 1: If (C! = 0) {alert ("check digit of the ID card is incorrect: the last digit should be: 0"); Return false;} break;
Case 2: If (C! = "X") {alert ("the last digit of the ID card is incorrect: X"); Return false;} break;
Case 3: If (C! = 9) {alert ("the last digit of the ID card is 9"); Return false;} break;
Case 4: If (C! = 8) {alert ("the last digit of the ID card is 8"); Return false;} break;
Case 5: If (C! = 7) {alert ("the last digit of the ID card is 7"); Return false;} break;
Case 6: If (C! = 6) {alert ("the last digit of the ID card is 6"); Return false;} break;
Case 7: If (C! = 5) {alert ("check digit of the ID card is incorrect: the last digit should be: 5"); Return false;} break;
Case 8: If (C! = 4) {alert ("the last digit of the ID card is 4"); Return false;} break;
Case 9: If (C! = 3) {alert ("check digit of the ID card is incorrect: the last digit should be: 3"); Return false;} break;
Case 10: If (C! = 2) {alert ("check digit of the ID card is incorrect: the last digit should be: 2"); Return false ;}
}
} Else {// 15-digit ID card number
If (! Isnumber (strno )){
Alert ("the ID card number is incorrect. The first 15 digits cannot contain English letters! ");
Return false;
}
}
Return true;
}

* Usage

HTML page addition

<SCRIPT type = "text/JavaScript" Language = "JavaScript" src = "JS/function. js"> </SCRIPT>

 

<Input name = "codeno" type = "text" id = "codeno" onblur = "ischinaidcard (this. Value)"/>

Or click <input onclick = "ischinaidcard (this. Value)" type = "button" id = "send" value = "Confirm submission"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.