Home > Web Front-end > JS Tutorial > JavaScript verification of ID number_javascript skills

JavaScript verification of ID number_javascript skills

WBOY
Release: 2016-05-16 16:11:25
Original
1170 people have browsed it

When we are building an Internet website, we often use our ID number when registering personal information. We need to verify the ID number, otherwise others will just enter the number and pass, which will make you feel that this website is very shitty.

There are rules for ID numbers.

Structure and form

 1. Number structure
The citizen identity number is a characteristic combination code, which consists of a seventeen-digit body code and a one-digit check code. The order from left to right is: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
2. Address code
Indicates the administrative division code of the county (city, banner, district) where the permanent residence of the coding object is located, and shall be implemented in accordance with the provisions of GB/T2260.
3. Date of birth code
Indicates the year, month, and day of birth of the encoding object. It is implemented in accordance with the provisions of GB/T7408. There is no separator between the year, month, and day codes.
4. Sequence code
Represents the sequence numbers assigned to people born in the same year, month, and day within the area identified by the same address code. The odd numbers of the sequence codes are assigned to males, and the even numbers are assigned to females.
5. Verification code
Based on the previous seventeen-digit code, the check code is calculated according to the ISO 7064:1983.MOD 11-2 check code.
Calculation method

1. Multiply the previous 17 digits of the ID number by different coefficients. The coefficients from the first to the seventeenth position are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2.
2. Add the results of multiplying these 17-digit numbers and coefficients.
3. Divide the added sum by 11. What is the remainder?
4. The remainder can only be 11 numbers: 0-1-2-3-4-5-6-7-8-9-10. The corresponding last ID number is 1-0-X-9-8-7-6-5-4-3-2.
5. From the above, we know that if the remainder is 3, the 18th digit of the ID card will appear as 9. If the corresponding number is 2, the last number on the ID card is the Roman numeral x.
For example: a man's ID card number is [53010219200508011x]. Let's see if this ID card is a legal ID card.
First, we get the sum of the products of the first 17 digits [(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)] is 189, then Divide 189 by 11 and the result is 189/11=17----2, which means the remainder is 2. Finally, through the corresponding rules, we can know that the check code corresponding to the remainder 2 is X. Therefore, it can be determined that this is a correct ID number.
The above is taken from Baidu Encyclopedia.

This is a relevant information picture found on the Internet.

According to the known information, we can write the internal implementation of this method in js. The verification of the first 17 digits is relatively easy to implement, so I won’t go into details and focus on the last digit of the check code.

Copy code The code is as follows:

// ID number verification
function isIdCard(cardid) {
// ID card regular expression (18 bits)
var isIdCard2 = /^[1-9]d{5}(19d{2}|[2-9]d{3})((0d)|(1[0-2]))(([0|1 |2]d)|3[0-1])(d{4}|d{3}X)$/i;
var stard = "10X98765432"; //The last ID number
var first = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //1-17 coefficient
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))) { //Verify whether the date is legal
          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 number
If (cardid[cardid.length - 1].toUpperCase() == last) {
         return true;
} else {
          return false;
}
}
//Convert date to string and return date format 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 '';
}

Only the 18-digit ID card is verified here, and the 15-digit first-generation ID card cannot be used.

The legality of the date is also verified here. For illegal dates such as 0230, 0431, etc., the verification will not pass.

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

Write a custom jquery validate verification method

Copy code The code is as follows:

// ID number verification
jQuery.validator.addMethod("isIdCard",
function(value, element) {
Return this.optional(element) || (isIdCard(value));
},
"Illegal ID number!");

Let’s take a simple demo and see how it works.

Copy code The code is as follows:

 
 
    
        
         <br>              身份证号校验<br>         
        
        
        


          

              
                                                                                                                                                                                                                                                                                                                                        
                                                  


                                                                                         
 



Verify with the ID number 53010219200508011X provided on Baidu Encyclopedia

The verification can pass, try changing x to 0

The verification failed, but the verification method we wrote succeeded! If you don’t believe me, try using your own ID number. It turns out that it is so easy to use js to verify the ID number.

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template