Home > Web Front-end > JS Tutorial > body text

js to verify the validity of ID card information_javascript skills

WBOY
Release: 2016-05-16 16:54:00
Original
930 people have browsed it

The following is the code for validating the ID card number using JS according to the encoding rules

IdCard-Validate.js code is as follows:

Copy code The code is as follows:

/**
* ID card 15-digit encoding rule: dddddd yymmdd xx p
* dddddd: Area code
* yymmdd: Date of birth
* xx: Sequential encoding, cannot be determined
* p : Gender, odd numbers are male, even numbers are female
*


* ID card 18-digit encoding rules: dddddd yyyymmdd xxx y
* dddddd: area code
* yyyymmdd: birth Year, month and day
* xxx: sequential code, cannot be determined, odd numbers are male, even numbers are female
* y: Check code, the value of this digit can be obtained by calculating the first 17 digits
*


* The weighting factor of the 18-digit number is (from right to left) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
* Verification digit Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
* Check digit calculation formula: Y_P = mod( ∑(Ai×Wi),11 )
* i is the 2...18 digits of the ID card number from right to left; Y_P is the position of the check code array where the foot check code is located
*
*/

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 the ID card array
if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){
return true;
}else {
return false;
}
} else {
return false;
}
}
/**
* Determine whether the last verification digit is correct when the ID number is 18 digits
* @param a_idCard ID card number array
* @return
*/
function isTrueValidateCodeBy18IdCard(a_idCard) {
var sum = 0; // Declare weighted sum variable
if (a_idCard[17].toLowerCase() == 'x') {
a_idCard[17] = 10;// Replace the last digit of the verification code with x with 10 to facilitate subsequent operations
}
for ( var i = 0; i < 17; i ) {
sum = Wi[i] * a_idCard[i];// Weighted sum
}
valCodePosition = sum % 11;/ / Get the verification code position
if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
} else {
return false;
}
}
/**
* Determine whether you are a boy or a girl based on your ID card
* @param idCard 15/18-digit ID number
* @return 'female'-female, 'male'-male
*/
function maleOrFemalByIdCard(idCard){
idCard = trim(idCard.replace(/ /g, ""));// Process the ID number. Include 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;
}
// The incoming characters can be directly treated as an array To handle
// 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 whether the birthday in the 18-digit ID card number is a valid birthday
* @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));
// Use getFullYear() here to get the year to avoid the Y2K problem
if (temp_date.getFullYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}
/**
* Verify whether the birthday in the 15-digit ID card number is a valid birthday
* @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));
// For your age in the old ID card, there is no need to consider the Y2K issue and use the getYear() method
if(temp_date.getYear()! =parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day)){
return false;
}else{
return true;
}
}
//Remove spaces at the beginning and end of the string
function trim(str) {
return str.replace(/(^s* )|(s*$)/g, "");
}


Updated version two
Copy code The code is as follows:

function checkIdcard(num)
{
num = num.toUpperCase();
//The ID number is 15 or 18 digits. When it is 15 digits, it is all numbers. The first 17 digits of the 18 digits are numbers. The last digit is a checksum. bit, which may be a number or the character X.
if (!(/(^d{15}$)|(^d{17}([0-9]|X)$)/.test(num)))
{
/ /alert('The length of the entered ID number is incorrect, or the number does not meet the regulations! The 15-digit number should be all digits, and the last digit of the 18-digit number can be a number or X. ');
return false;
}
//The check digit is generated according to the regulations of ISO 7064:1983.MOD 11-2, and X can be considered as the number 10.
//The following analyzes the 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 whether the birthday date is correct
var dtmBirth = new Date('19' 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('The date of birth in the entered ID number is incorrect!');
return false ;
}
else
{
//Convert the 15-digit ID card to 18-digits
//The check digit is generated in accordance with ISO 7064:1983.MOD 11-2, X Think of it as 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) '19' num.substr(6, num.length - 6);
for(i = 0; i < 17 ; 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 whether 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('The date of birth in the entered ID number is incorrect! ');
return false;
}
else
{
//Check whether the check code of the 18-digit ID card is correct.
//The check digit is generated in accordance with ISO 7064:1983.MOD 11-2, and X can be considered as 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 < 17; i )
{
nTemp = num.substr(i, 1) * arrInt[ i];
}
valnum = arrCh[nTemp % 11];
if (valnum != num.substr(17, 1))
{
//alert('18 bit The verification code of the ID card is incorrect! It should be: ' valnum);
return false;
}
return true;
}
}
return false;
}
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!