JS Verify ID number authenticity implementation code

Source: Internet
Author: User
Tags ereg getdate regular expression

The ID card number is divided into two, the old one is 15 digits, the new one is 18 digits. ID Card 15-bit encoding rule: dddddd yymmdd xx P dddddd: Region code YYMMDD: Date of birth XX: Sequence class code, cannot determine p: sex, odd for male, even for female; ID 18-bit encoding rule: dddddd yy YYMMDD XXX dddddd: Area code yyyymmdd: birth date XXX: Sequence class code, cannot determine, odd number is male, even is female y: Check code, this bit value can be obtained by the first 17 digits calculation, the formula of calculation see program, some need to use constant : 18-digit number weighting factor (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1] Verify bit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2] check digit formula: y_p = mod (∑ (AIXWI), one) I for the ID number from the right to the left number of 2...18 bit; Y_p is the location of the check code array for the foot check code.

The code is as follows Copy Code




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


function Idcardvalidate (idcard) {


Idcard = Trim (idcard.replace (//g, "")); Remove string and tail space


if (idcard.length = = 15) {


Return Isvaliditybrithby15idcard (Idcard); Verification of 15-bit ID card


else if (idcard.length = 18) {


var a_idcard = Idcard.split (""); Get an array of IDs


if (Isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {//Perform basic validation of 18-bit IDs and 18th-bit verification


return true;


}else {


return false;


}


} else {


return false;


}


}


/**


* Determine whether the final verification bit is correct when the ID number is 18 digits


* @param a_idcard ID number array


* @return


*/


function Istruevalidatecodeby18idcard (a_idcard) {


var sum = 0; Declaring a weighted sum variable


if (a_idcard[17].tolowercase () = = ' X ') {


A_IDCARD[17] = 10; Replace the last-bit X code with 10 to facilitate subsequent operations


}


for (var i = 0; i < i++) {


Sum + + wi[i] * A_idcard[i]; Weighted summation


}


valcodeposition = sum% 11; Get the location of the verification code


if (a_idcard[17] = = Validecode[valcodeposition]) {


return true;


} else {


return false;


}


}


/**


* Verify that the birthdays in the 18-digit ID number are valid birthdays


* @param idcard 18-digit ID card string


* @return


*/


function Isvaliditybrithby18idcard (IDCARD18) {


var year = idcard18.substring (6,10);


var month = idcard18.substring (10,12);


var day = idcard18.substring (12,14);


var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day));


We use getFullYear () to get the year to avoid the millennium bug problem.


if (Temp_date.getfullyear ()!=parsefloat (year)


|| Temp_date.getmonth ()!=parsefloat (month)-1


|| Temp_date.getdate ()!=parsefloat (day)) {


return false;


}else{


return true;


}


}


/**


* Verify that the birthdays in the 15-digit ID number are valid birthdays


* @param idCard15 15-digit ID card string


* @return


*/


function Isvaliditybrithby15idcard (IDCARD15) {


var year = idcard15.substring (6,8);


var month = idcard15.substring (8,10);


var day = idcard15.substring (10,12);


var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day));


The GetYear () method is used for your age in the old ID card without considering the millennium bug problem.


if (Temp_date.getyear ()!=parsefloat (year)


|| Temp_date.getmonth ()!=parsefloat (month)-1


|| Temp_date.getdate ()!=parsefloat (day)) {


return false;


}else{


return true;


}


}


Remove string and tail space


function Trim (str) {


Return Str.replace (/(^s*) | ( s*$)/g, "");


}

The most concise example

The code is as follows Copy Code


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 Iscardid (sId) {
var isum=0;
var info= "";
if (!/^d{17} (d|x) $/i.test (sId)) return "The ID card length or format error you entered";
Sid=sid.replace (/x$/i, "a");
if (Acity[parseint (Sid.substr (0,2))]==null) return "illegal in your identity card 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 "The date of birth on the ID card is illegal";
for (var i = 17;i>=0;i-) Isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), 11);
if (isum%11!=1) return "The ID number you entered is illegal";
Return True;//acity[parseint (Sid.substr (0,2))]+ ", +sbirthday+", "+ (SID.SUBSTR (16,1)%2?" Male ":" female ")
}

JS Code instance (can verify true and false ID card)

The code is as follows Copy Code

var idcardnoutil = {
* * Province, the Central Municipality code table * *
Provinceandcitys: {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: "Macau", 91: "Abroad"},

/* per-weighting factor * *
Powers: ["7", "9", "10", "" 5 "," 8 "," 4 "," 2 "," 1 "," 6 "," 3 "," 7 "," 9 "," 10 "," 5 "," 8 "," 4 "," 2 "],

/* 18th School Inspection Code * *
Paritybit: ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"],

* * Sex *
Genders: {male: "male", Female: "Female"},

/* Verify Address code * *
Checkaddresscode:function (Addresscode) {
var check =/^[1-9]d{5}$/.test (Addresscode);
if (!check) return false;
if (Idcardnoutil.provinceandcitys[parseint (addresscode.substring (0,2))]) {
return true;
}else{
return false;
}
},

/* Check Date code * *


Checkbirthdaycode:function (Birdaycode) {


var check =/^[1-9]d{3} ((0[1-9]) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1]) $/.test (Birdaycode);


if (!check) return false;


var yyyy = parseint (birdaycode.substring (0,4), 10);


var mm = parseint (birdaycode.substring (4,6), 10);


var dd = parseint (birdaycode.substring (6), 10);


var xdata = new Date (YYYY,MM-1,DD);


if (XData > New Date ()) {


Return false;//birthday cannot be greater than the current date


}else if ((xdata.getfullyear () = = yyyy) && (xdata.getmonth () = = mm-1) && (xdata.getdate () = = dd) ){


return true;


}else{


return false;


}


},





/* Calculate the School Check code * *


Getparitybit:function (Idcardno) {


var id17 = idcardno.substring (0,17);


/* Weighted *


var power = 0;


for (Var i=0;i<17;i++) {


Power + + parseint (Id17.charat (i), ten) * parseint (idcardnoutil.powers[i));


}


/* Take die/*


var mod = power% 11;


return Idcardnoutil.paritybit[mod];


},





/* Verify the School check code * *


Checkparitybit:function (Idcardno) {


var paritybit = Idcardno.charat. toUpperCase ();


if (idcardnoutil.getparitybit (idcardno) = = Paritybit) {


return true;


}else{


return false;


}


},

 /* Verify 15-bit or 18-bit ID number */
  checkidcardno:function (idcardno) {
   // Basic checksum for 15-bit and 18-bit ID numbers
    var check =/^d{15}| ( D{17} (d|x| X)) $/.test (Idcardno);
    if (!check) return false;
   //Judgment length 15-bit or 18-bit  
    if (idcardno.length==15) {
         return Idcardnoutil.check15idcardno (Idcardno);
   }else if (idcardno.length==18) {
        return Idcardnoutil.check18idcardno (Idcardno);
   }else{
        return false;
   }
 },

 //Verify 15-bit ID number
  check15idcardno:function (idcardno) {
    //15-bit ID number basic check
     var check =/^[1-9]d{7} ((0[1-9)) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1]) D{3}$/.test (Idcardno);  
     if (!check) return false;
     //Checksum address code
     var addresscode = idcardno.substring (0,6);
     Check = Idcardnoutil.checkaddresscode (Addresscode);
     if (!check) return false;
     var birdaycode = ' n ' + idcardno.substring (6,12);
    //Checksum date code
     return Idcardnoutil.checkbirthdaycode (birdaycode );
 },

 //verify 18-bit ID number
  check18idcardno:function (idcardno) {
    // Basic format verification for 18-bit ID number
     var check =/^[1-9]d{5}[1-9]d{3} ((0[1-9)) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1]) D{3} (d|x| X) $/.test (Idcardno);
     if (!check) return false;
    //Checksum address code
     var addresscode = idcardno.substring (0,6);
     check = Idcardnoutil.checkaddresscode (Addresscode);
     if (!check) return false;
    //Checksum date code
     var birdaycode = idcardno.substring (6,14);
     check = Idcardnoutil.checkbirthdaycode (Birdaycode);
     if (!check) return false;
    //Verifying school Check code   
     return Idcardnoutil.checkparitybit ( Idcardno);  
 },

Formatedatecn:function (Day) {
var yyyy =day.substring (0,4);
var mm = day.substring (4,6);
var dd = day.substring (6);
return yyyy + '-' + mm + '-' + dd;
},

Get information


Getidcardinfo:function (Idcardno) {


var idcardinfo = {


Gender: "",//gender


Birthday: ""//Birth date (YYYY-MM-DD)


};


if (idcardno.length==15) {


var aday = ' n ' + idcardno.substring (6,12);


IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);


if (parseint (Idcardno.charat (14))%2==0) {


Idcardinfo.gender=idcardnoutil.genders.female;


}else{


Idcardinfo.gender=idcardnoutil.genders.male;


}


}else if (idcardno.length==18) {


var aday = idcardno.substring (6,14);


IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);


if (parseint (Idcardno.charat (16))%2==0) {


Idcardinfo.gender=idcardnoutil.genders.female;


}else{


Idcardinfo.gender=idcardnoutil.genders.male;


}





}


return idcardinfo;


},





/*18 15-bit * *


Getid15:function (Idcardno) {


if (idcardno.length==15) {


return idcardno;


}else if (idcardno.length==18) {


Return idcardno.substring (0,6) + idcardno.substring (8,17);


}else{


return null;


}


},





/*15 18-bit * *


Getid18:function (Idcardno) {


if (idcardno.length==15) {


var id17 = idcardno.substring (0,6) + ' + ' + idcardno.substring (6);


var paritybit = idcardnoutil.getparitybit (ID17);


return id17 + paritybit;


}else if (idcardno.length==18) {


return idcardno;


}else{


return null;


}


}


};

ID Card number Verification
JQuery.validator.addMethod ("Idcardno", function (value, Element) {
return this.optional (Element) | | Idcardnoutil.checkidcardno (value);
"Please specify a valid ID number.");

Get ID Information
var idcardinfo = Idcardnoutil.getidcardinfo (Idcardno);
Alert (Idcardinfo.gender + "|" + Idcardinfo.birthday);

Example 3

The code is as follows Copy Code

<script>


Authentication ID Number method


var test=function (Idcard) {


var errors=new Array ("Verify through!", "ID card number is not correct!", "ID number date of birth out of range or contain illegal characters!", "ID card number Error!", "Identity card area illegal!");


var area={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",: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"}


var Idcard,y,jym;


var s,m;


var idcard_array = new Array ();


Idcard_array = Idcard.split ("");


if (Area[parseint (Idcard.substr (0,2))]==null) return errors[4];


Switch (idcard.length) {


Case 15:


if ((parseint (Idcard.substr (6,2)) +1900% 4 = 0 | | ((parseint (Idcard.substr (6,2)) +1900% = = 0 && (parseint (Idcard.substr (6,2)) +1900)% 4 = 0)) {


Ereg =/^[1-9][0-9]{5}[0-9]{2} (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}$/;//Test Birth date legality


}


else{


Ereg =/^[1-9][0-9]{5}[0-9]{2} (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}$/;//test date of birth legality


}


if (Ereg.test (Idcard))


return errors[0];


Else


return errors[2];


Break


Case 18:


if (parseint (Idcard.substr (6,4))% 4 = 0 | | (parseint (Idcard.substr (6,4))% = = 0 && parseint (idcard.substr (6,4))%4 = = 0)) {


Ereg =/^[1-9][0-9]{5}19[0-9]{2} (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}[0-9xx]$/;//validity of a leap year birth date regular expression


}


else{


Ereg =/^[1-9][0-9]{5}19[0-9]{2} (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}[0-9xx]$/;//excepting the legality of the date of birth regular expression


}


if (Ereg.test (Idcard)) {


S = (parseint (idcard_array[0]) + parseint (idcard_array[10])) * 7 + (parseint (idcard_array[1)) + parseint (idcard_array[11 ]) * 9 + (parseint (idcard_array[2]) + parseint (idcard_array[12))) * + (parseint (idcard_array[3)) + parseint (Idcard_arr AY[13])) * 5 + (parseint (idcard_array[4)) + parseint (idcard_array[14])) * 8 + (parseint (idcard_array[5)) + parseint (idcard _ARRAY[15])) * 4 + (parseint (idcard_array[6)) + parseint (idcard_array[16])) * 2 + parseint (idcard_array[7)) * 1 + parseInt (Idcard_array[8]) * 6 + parseint (idcard_array[9]) * 3;


Y = S% 11;


M = "F";


Jym = "10x98765432";


M = Jym.substr (y,1);


if (M = = idcard_array[17])


return errors[0];


Else


return errors[3];


}


Else


return errors[2];


Break


Default


return errors[1];


Break


}


}


Test code


var theid=prompt ("Please enter the ID number", "15-bit or 18-bit");


if (theid!= "") {


document.write (Test (Theid));


}else{


Theid=prompt ("Please enter ID number", "15 or 18 digits");


}


</script>

A gender determination based on the ID number

The code is as follows Copy Code

/**


* Judging by ID card is male or female


* @param idcard 15/18-digit ID number


* @return ' female '-female, ' male '-male


*/


function Maleorfemalbyidcard (idcard) {


Idcard = Trim (idcard.replace (//g, "")); Handle the ID card number. Includes spaces between characters.


if (idcard.length==15) {


if (idcard.substring (14,15)%2==0) {


return ' female ';


}else{


return ' male ';


}


}else if (idcard.length ==18) {


if (idcard.substring (14,17)%2==0) {


return ' female ';


}else{


return ' male ';


}


}else{


return null;


}


}

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.