JavaScript verification ID Number _javascript tips

Source: Internet
Author: User
Tags getdate

When we do the Internet site, registration of personal data, often need to use the ID card, we need to verify the identity card, or other people casually lose a number on the pass, so that you feel this site to do very shit.

There is a rule in the ID card number.

Structure and form

1. The structure of the number
The citizenship number is a characteristic combination code, which consists of a 17 digit ontology code and a check code. The order is from left to right: six digit address code, eight digit birth date code, three digit sequential code and one digit parity code.
2. Address Code
The administrative division code of the county (city, Flag, district) of the resident account of the encoding object is executed according to the GB/T2260 regulations.
3. Birth date Code
Indicates the year, month, and day of the encoding object's birth, which is executed according to gb/t7408, and the year, month, and day codes are not separated by delimiters.
4. Sequential code
Indicates the sequence number assigned to a person born in the same same address code, the same month and the same day, and the odd distribution of order codes to men, even to females.
5. Check code
According to the preceding 17 digit code, the inspection code calculated according to the ISO 7064:1983.mod 11-2 check code.
Calculation method

1, the previous ID number 17 digits multiplied by different coefficients. The coefficients from first to 17th digits are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2.
2, add these 17-digit numbers and coefficients by multiplying the results.
3, with added out and divided by 11, see the remainder is how much?
4, the remaining number may only have 0-1-2-3-4-5-6-7-8-9-10 these 11 digits. The corresponding number of the last ID card is 1-0-x-9-8-7-6-5-4-3-2.
5, through the above learned that if the remainder is 3, will be in the ID card 18th digits appear on the 9. If the corresponding number is 2, the last number of the ID card is the Roman numeral x.
For example: A man's ID number is "53010219200508011x", let's see if this ID card is a legal ID.
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) + (5*9) + (0*10) + (8*5) + (0*8) + (1* 4) + (1*2) "is 189, and then with 189 divided by 11 The result is 189/11=17----2, which means the remaining number is 2. Finally, the corresponding rule can know that the remainder 2 corresponding to the test code is x. So, you can decide that this is a correct ID number.
Above excerpt from Baidu Encyclopedia.

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

Based on the known data, we can write the internal implementation of this method in JS. The first 17-bit verification is easier to implement, I will not say more, focus on the last one of the check code.

Copy Code code as follows:

Identification Number Verification
function Isidcard (cardid) {
Identity card Regular expression (18-bit)
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 last ID number.
var-i = [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 + '/' + ')) {//Verify 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]; Figure out the last I.D. number.
if (cardid[cardid.length-1].touppercase () = last) {
return true;
} else {
return false;
}
}
Date-turn string 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 ";
}

Here only verify 18 ID cards, 15-bit ID card can not be used.

It also validates the legality of the date, such as 0230,0431 and other illegal dates, and validation is not passed.

We can also add this method to the jquery validate to facilitate validation.

Write a custom jquery Validate school recipe method

Copy Code code as follows:

Identification Number Verification
JQuery.validator.addMethod ("Isidcard",
function (value, Element) {
return this.optional (Element) | | (Isidcard (value));
},
"The ID number is illegal! ");

Let's have a Jane's demo and see how it works.

Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>
Verification of ID card number
</title>
<script src= "Http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" >
</script>
<script src= "Http://www.w3cschool.cc/try/demo_source/static/js/jquery.validate.js" >
</script>
<script type= "Text/javascript" >
$ (function () {
$ ("#form1"). Validate ({
Rules: {
Txtidcard: "Isidcard"
}
});
});
Identification Number Verification
function Isidcard (cardid) {
Identity card Regular expression (18-bit)
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 last ID number.
var-i = [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 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]; Figure out the last I.D. number.
if (cardid[cardid.length-1].touppercase () = last) {
return true;
} else {
return false;
}
}
Date-turn string 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 ";
}
jquery Validate ID Number verification
JQuery.validator.addMethod ("Isidcard",
function (value, Element) {
return this.optional (Element) | | (Isidcard (value));
},
"The ID number is illegal! ");
</script>
<body>
<form id= "Form1" method= "Get" action= "" >
<input type= "text" id= "Txtidcard" name= "Txtidcard"/>
<p>
<input class= "Submit" type= "Submission" value= "submitted"/>
</p>
</form>
</body>

With the Baidu encyclopedia on the provision of the ID card number 53010219200508011X verification

Verify that you can go through and change X to 0 try

Checksum is not passed, we write the verification method succeeded! Do not believe that you use your own ID number to try. The original use of JS to verify the ID number so easy.

The above is the entire content of this article, I hope you can enjoy.

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.