The following is based on the ID number coding rules, using JS to validate the code
The Idcard-validate.js code is as follows:
Copy Code code as follows:
/**
* ID Card 15-bit coding rule: dddddd YYMMDD xx p
* DDDDDD: Area code
* YYMMDD: Date of birth
* XX: Sequential class code, cannot be determined
* p: Sex, odd for male, even for female
* <p/>
* ID card 18-bit coding rule: dddddd yyyymmdd xxx y
* DDDDDD: Area code
* YYYYMMDD: Date of birth
* XXX: Sequence class Code, cannot be determined, odd number is male, even number is female
* Y: Check code, which can be obtained by the first 17 digits
* <p/>
* 18-bit 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 calculation formula: y_p = mod (∑ (AIXWI), 11)
* I for the ID card number from the right to the left of the 2...18 bit; Y_p for the Foot check code is the location of the check Code array
*
*/
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, ""));
if (idcard.length = = 15) {
Return Isvaliditybrithby15idcard (Idcard);
else if (idcard.length = 18) {
var a_idcard = Idcard.split ("");//Get ID card array
if (Isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {
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;//replaces the last bit X's captcha with 10 for subsequent operations
}
for (var i = 0; i < i++) {
Sum + = wi[i] * a_idcard[i];//weighted sum
}
valcodeposition = sum% 11;//Get the location of the verification code
if (a_idcard[17] = = Validecode[valcodeposition]) {
return true;
} else {
return false;
}
}
/**
* 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, ""))//To handle the ID 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;
}
Can be processed directly as an array for incoming characters
if (idcard.length==15) {
Alert (idcard[13]);
if (idcard[13]%2==0) {
return ' female ';
}else{
return ' male ';
// }
}else if (idcard.length==18) {
Alert (idcard[16]);
if (idcard[16]%2==0) {
return ' female ';
}else{
return ' male ';
// }
}else{
return null;
// }
}
/**
* 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, "");
}
Updated Version Two
Copy Code code as follows:
function Checkidcard (num)
{
num = Num.touppercase ();
The ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits are the first 17 digits, and the last is a check digit, possibly a number or a character X.
if (!) ( /(^\d{15}$) | (^\d{17} ([0-9]| X) $)/.test (num))
{
Alert (' Enter the 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 to be the number 10.
The following analysis of birth date and check digit respectively
var len, re;
len = num.length;
if (len = = 15)
{
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 true;
}
}
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 true;
}
}
return false;
}