JavaScript ID number Validation implementation program

Source: Internet
Author: User
Tags getdate

The address code represents the administrative division code of the county (city, Flag, district) of the resident residence of the encoding object.
The Birth date code represents the year, month, and day of the encoding object's birth, where the year is represented by a four-digit number, and the year, month, and day are not separated.
The sequential code represents the sequence number of persons born in the same same address code, in the range of the area identified by the same one. The odd-numbered distribution of sequential codes to men

even to women.

The check code is based on the front 17 digit code, according to the ISO 7064:1983.mod 11-2 check code to calculate the inspection code.

Method of Birth Date calculation


The 15-digit ID code first expands the birth year to 4 digits, simply by adding a 19 or 18, which includes all persons born in 1800-1999;
2000 years after the birth of the affirmation is 18-bit without this trouble, as for 1800 years ago, then what should not have ID number this stuff


The rules of the ID card number

1, 15-digit ID card number composition:
Ddddddyymmddxxs a total of 15 places, of which:
The DDDDDD is a 6-bit local code, based on which the 6-bit can obtain the location of the ID number.
YY is a 2-bit year code, is the identity card holder of the year of origin.
MM is a 2-bit month code, is the identity card holder's birth month.
DD is a 2-digit date code, which is the birth date of the identity card holder.
These 6 together form the date of birth of the identity card holder.
XX is a 2-bit sequential code, this is a random number.
S is a 1-digit sex code, with odd numbers representing males and even women.

2, 18-digit ID card number composition:
DDDDDDYYYYMMDDXXSP a total of 18 places, of which:
The other parts are the same as the 15-bit. The year code is upgraded from the original 2 to the 4-bit. The last one is a check digit.
The validation rules are:
(1) The weighted summation formula of 17-digit body code
S = SUM (Ai * Wi), i = 0, ..., 16, first the sum of the first 17 digits
Ai: Indicates the ID number value of position I
Wi: A weighted factor representing position I
Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4-2
(2) Computational model
Y = mod (S, 11)
(3) The corresponding check code is obtained through the model
Y:0 1 2 3 4 5 6 7 8 9 10
Check code: 1 0 X 9 8 7 6 5 4 3 2

That is, if you get the remainder of 1, the final check digit p should be the corresponding 0. If the check digit is not, the ID number is incorrect

1, a simple regular expression:

The code is as follows Copy Code

(1) Preg_match ("/^ (/d{18,18}|/d{15,15}|/d{17,17}x) $/", $id _card)
(2) Preg_match ("/^ (/d{6})" (18|19|20)? /D{2}) ([01]/d) ([0123]/d) (/d{3}) (/d| X)? $/", $id _card)
(3) Preg_match ("/(^/d{15}$/) | ( /D{17} (?:/ D|x| X) $/), $id _card)

2. Complex and rigorous verification:

This verifies 15-bit and 18-bit IDs and includes validation of birthdays and check-bits.
If you are interested, you can also add the identity card location of the verification, that is, the first 6 digits of some of the legal number is illegal.

The code is as follows Copy Code

Function Isidcardno (num)
{  
          num = Num.touppercase (); 
        //ID number is 15 digits or 18 digits, 15 digits are all digits, 18 Digits, the first 17 digits, and the last one is a check digit, possibly a number or a character X.   
          if (! /(^/d{15}$) | (^/d{17} ([0-9]| X) $)/.test (num))   
          {
                alert (' Input ID number is not the right length, or the number does not meet the requirements! /n15 digit number should be full, 18 digits can be the lowest number or X. ');
              return false;
        } The
//check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered the number 10.
//below analyze birth date and check digit respectively
Var len, re;
Len = num.length;
if (len = =)
{
Re = new RegExp (/^ (/d{6}) (/d{2}) (/d{2}) (/d{2})/d{3);
var arrsplit = Num.match (re);

Check if the birthday date is correct
var Dtmbirth = new Date (' ' + arrsplit[2] + '/' + arrsplit[3] + '/' + arrsplit[4] ";
var bgoodday;
Bgoodday = (dtmbirth.getyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) ;& (dtmbirth.getdate () = = number (arrsplit[4]));
if (!bgoodday)
{
Alert (' entered the ID number in the wrong birth date! ');
return false;
}
Else
{
Turn 15 ID cards into 18-bit
The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10.
var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
var ntemp = 0, I;
num = num.substr (0, 6) + ' n ' + num.substr (6, num.length-6);
for (i = 0; i < i + +)
{
Ntemp + + NUM.SUBSTR (i, 1) * arrint[i];
}
num + + arrch[ntemp% 11];
return num;
}
}
if (len = = 18)
{
Re = new RegExp (/^ (/d{6}) (/d{4}) (/d{2}) (/d{2}) (/d{3}) ([0-9]| X) $/);
var arrsplit = Num.match (re);

Check if the birthday date is correct
var Dtmbirth = new Date (arrsplit[2] + "/" + arrsplit[3] + "/" + arrsplit[4]);
var bgoodday;
Bgoodday = (dtmbirth.getfullyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) && (dtmbirth.getdate () = = number (arrsplit[4]);
if (!bgoodday)
{
Alert (Dtmbirth.getyear ());
Alert (arrsplit[2]);
Alert (' entered the ID number in the wrong birth date! ');
return false;
}
Else
{
Verify that the check code for the 18-bit ID card is correct.
The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10.
var valnum;
var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
var ntemp = 0, I;
for (i = 0; i < i + +)
{
Ntemp + + NUM.SUBSTR (i, 1) * arrint[i];
}
Valnum = arrch[ntemp% 11];
if (Valnum!= num.substr (17, 1))
{
Alert (' 18-bit ID card is not the correct check code! should be: ' + valnum ';
return false;
}
return num;
}
}
return false;
}
3, the strict verification:

<script>
var acity={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: "Foreign"}

function Cidinfo (sId) {
var isum=0
var info= ""
if (!/^d{17} (d|x) $/i.test (sId)) return false;
Sid=sid.replace (/x$/i, "a");
if (Acity[parseint (Sid.substr (0,2))]==null) return "Error: illegal area";
Sbirthday=sid.substr (6,4) + "-" +number (Sid.substr (10,2)) + "-" +number (Sid.substr (12,2));
var d=new Date (Sbirthday.replace (/-/g, "/")
if (sbirthday!= (d.getfullyear () + "-" + (D.getmonth () +1) + "-" + d.getdate ()) return "Error: illegal Birthday";
for (var i = 17;i>=0;i-) Isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), 11)
if (isum%11!=1) return "ERROR: illegal license Number";
Return Acity[parseint (Sid.substr (0,2))]+ ", +sbirthday+", "+ (SID.SUBSTR (16,1)%2?" Male ":" female ")
}
document.write (Cidinfo ("380524198002300016"), "<br/>");
document.write (Cidinfo ("340524198002300019"), "<br/>")
document.write (Cidinfo ("340524197711111111"), "<br/>")
document.write (Cidinfo ("34052419800101001x"), "<br/>");
</script>

JS ID number Validity Verification

The code is as follows Copy Code

function Idcardvalidate (idcard) {
var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];//weighting factor
var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; ID card Verification bit value. 10 represents X
var sum = 0;
var isvaliditybrith = function (year,month,day) {
var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day));
if (year.length = = 2) {
var temp_year = Temp_date.getyear ();
}else if (year.length = = 4) {
var temp_year = Temp_date.getfullyear ();
}else{
return false;
}
if (Temp_year!= parsefloat (year)
|| Temp_date.getmonth ()!= parsefloat (month)-1
|| Temp_date.getdate ()!= parsefloat (day)) {
return false;
}else{
return true;
}
}

Idcard = idcard.replace (//g, ""). Replace (/(^s*) | ( s*$)/g, "");
if (idcard.length = = 15) {
var year = idcard.substring (6,8);
var month = idcard.substring (8,10);
var day = idcard.substring (10,12);
Return Isvaliditybrith (Year,month,day);
}
if (Idcard.length!=) return false;
var a_idcard = Idcard.split ("");
if (a_idcard[17].tolowercase () = = ' x ') a_idcard[17] = 10;
for (var i = 0; i < i++) {
Sum + + wi[i] * A_idcard[i];
}
valcodeposition = sum% 11; Get the location of the verification code
if (a_idcard[17]!= validecode[valcodeposition]) return false;
var year = idcard.substring (6,10);
var month = idcard.substring (10,12);
var day = idcard.substring (12,14);
Return Isvaliditybrith (Year,month,day);
}

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.