Javascript verification ID card _ javascript skills

Source: Internet
Author: User
This article mainly introduces the javascript verification ID card number, which is very detailed. For more information, see ID card numbers, we need to verify your ID card. Otherwise, someone else will pass the verification by entering a number, making you feel that this website is doing a lot of work.

There are rules for ID card numbers.

Structure and form

1. number structure
A citizenship number is a combination of features and consists of a 17-digit Ontology code and a verification code. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
2. address code
Indicates the Administrative Code of the county (city, flag, District) where the resident account of the encoding object is located, which is executed according to GB/T2260.
3. date of birth
Indicates the year, month, and day when the encoding object is born. the code is executed according to GB/T7408, and there is no separator between the year, month, and day codes.
4. sequence code
Indicates the sequence number of the person born on the same year, the same month, and the same day within the region specified by the same address code. the odd number of the sequence code is allocated to men, and the even number is allocated to women.
5. verification code
According to the preceding 17-digit code, the verification code is calculated based on the ISO 7064: 1983.MOD 11-2 verification code.
Calculation method

1. multiply the 17 digits of the previous ID card number by different coefficients. The coefficients from the first digit to the second digit are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2.
2. add the result of multiplying the 17-digit number and the coefficient.
3. how much is the remainder by adding and dividing by 11?
4. The remainder may only contain the 11 numbers 0-1-2-3-4-5-6-7-8-9-10. The numbers corresponding to the last ID card respectively are 1-0-X-9-8-7-6-5-4-3-2.
5. if the remainder is 3, 9 is displayed on the 18th digits of the ID card. If the corresponding number is 2, the last digit of the ID card is the Roman numerals x.
For example, if the ID number of a male is 53010219200508011x, check whether the ID card is valid.
First, we get the product of the first 17 bits and [(5*7) + (3*9) + (0*10) + (1*5) + (0*8) + (2*4) + (1*2) + (9*1) + (2*6) + (0*3) + (0*7) + (5x9) + (0x10) + (8x5) + (0x8) + (1x4) + (1x2)] is 189, then divide 189 by 11 and the result is 189/11 = 17----2, that is, the remaining number is 2. Finally, we can use the corresponding rules to know that the check code corresponding to remainder 2 is X. Therefore, it can be determined that this is a correct ID card number.
The above is taken from Baidu Encyclopedia.

This is a picture of the relevant information found on the Internet.

Based on the known information, we can write the internal implementation of this method using js. The first 17-digit verification is easier to implement. I will not talk about it much, but I will focus on the last verification code.

The code is as follows:


// ID card number verification
Function isIdCard (cardid ){
// ID card regular expression (18 digits)
Var isIdCard2 =/^ [1-9] \ d {5} (19 \ d {2} | [2-9] \ d {3}) (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) (\ d {4} | \ d {3} X) $/I;
Var stard = "10X98765432"; // The number of the last ID card
Var first = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // coefficient 1-17
Var sum = 0;
If (! IsIdCard2.test (cardid )){
Return false;
}
Var year = cardid. substr (6, 4 );
Var month = cardid. substr (10, 2 );
Var day = cardid. substr (12, 2 );
Var birthday = cardid. substr (6, 8 );
If (birthday! = DateToString (new Date (year + '/' + month + '/' + day) {// check whether the Date is valid
Return false;
}
For (var I = 0; I <cardid. length-1; I ++ ){
Sum + = cardid [I] * first [I];
}
Var result = sum % 11;
Var last = stard [result]; // The calculated last ID card number.
If (cardid [cardid. length-1]. toUpperCase () = last ){
Return true;
} Else {
Return false;
}
}
// Returns a date in the format of 20080808.
Function dateToString (date ){
If (date instanceof Date ){
Var year = date. getFullYear ();
Var month = date. getMonth () + 1;
Month = month <10? '0' + month: month;
Var day = date. getDate ();
Day = day <10? '0' + day: day;
Return year + month + day;
}
Return '';
}

Here, only the 18-digit ID card is verified. the 15-digit ID card cannot be used.

The validity of the date is also verified here, such as 0230,0431 and other invalid dates. the verification will not pass.

We can also add this method to jquery validate for verification.

Write a custom jquery validate verification method

The code is as follows:


// ID card number verification
JQuery. validator. addMethod ("isIdCard ",
Function (value, element ){
Return this. optional (element) | (isIdCard (value ));
},
"The ID card number is invalid! ");

Here is a simple demo to see how it works.

The code is as follows:






<Br/> ID card number verification <br/>

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.