Javascript to verify the validity of ID card information _ javascript skills

Source: Internet
Author: User
Verify the validity of the ID card number based on the encoding rules. The specific implementation is as follows. You can refer to the following code to verify the validity of the ID card number based on the ID card number encoding rules.

The IdCard-Validate.js code is as follows:

The Code is as follows:


/**
* ID card 15-bit encoding rule: dddddd yymmdd xx p
* Dddddd: Location Code
* Yymmdd: Date of birth
* Xx: sequence class encoding, which cannot be determined
* P: gender. The odd number is male and the even number is female.
*


* ID card 18-bit encoding rule: dddddd yyyymmdd xxx y
* Dddddd: Location Code
* Yyyymmdd: Date of birth
* Xxx: sequence class code, which cannot be determined. The odd number is male and the even number is female.
* Y: Check Code. The value can be obtained through the first 17 digits.
*


* The 18-digit number weighting factor 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]
* Formula for calculating the checkpoint: Y_P = mod (Σ (Ai × Wi), 11)
* I is the 2 .. 18 bits of the ID card number from right to left; Y_P is the position of the checkcode array where the ankle checkcode 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 bid 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 (""); // obtain the ID card Array
If (isValidityBrithBy18IdCard (idCard) & isTrueValidateCodeBy18IdCard (a_idCard )){
Return true;
} Else {
Return false;
}
} Else {
Return false;
}
}
/**
* Check whether the last verification digit is correct when the ID card number is 18
* @ Param a_idCard ID card number Array
* @ Return
*/
Function isTrueValidateCodeBy18IdCard (a_idCard ){
Var sum = 0; // declare the weighted sum variable
If (a_idCard [17]. toLowerCase () = 'X '){
A_idCard [17] = 10; // Replace the last x verification code with 10 for subsequent operations
}
For (var I = 0; I <17; I ++ ){
Sum + = Wi [I] * a_idCard [I]; // weighted sum
}
ValCodePosition = sum % 11; // the location where the verification code is obtained
If (a_idCard [17] = ValideCode [valCodePosition]) {
Return true;
} Else {
Return false;
}
}
/**
* Identify a male or female by ID card
* @ Param idCard: 15/18-digit ID card number
* @ Return 'female '-female, 'male'-male
*/
Function maleOrFemalByIdCard (idCard ){
IdCard = trim (idCard. replace (// g, ""); // process the ID card number. Contains 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 input characters can be processed as arrays.
// 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 book 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 ));
// GetFullYear () is used to obtain the year to avoid the millennium bug.
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 birthday in the 15-digit ID card number is a valid birthday.
* @ Param idCard15 character book 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 ));
// Use the getYear () method if you are older than your ID card.
If (temp_date.getYear ()! = ParseFloat (year)
| Temp_date.getMonth ()! = ParseFloat (month)-1
| Temp_date.getDate ()! = ParseFloat (day )){
Return false;
} Else {
Return true;
}
}
// Remove the leading and trailing spaces of the string
Function trim (str ){
Return str. replace (/(^ \ s *) | (\ s * $)/g ,"");
}


Update Version 2

The Code is as follows:


Function checkIdcard (num)
{
Num = num. toUpperCase ();
// The ID card number is 15 or 18 digits. The 15 digits are all digits. the first 17 digits are digits. the last digit is the check digit, which may be digits or characters X.
If (! (/(^ \ D {15} $) | (^ \ d {17} ([0-9] | X) $)/. test (num )))
{
// Alert ('the length of the ID card number entered is incorrect, or the number does not comply with the regulations! The 15-digit number must be a number, and the last 18-digit number can be a number or X. ');
Return false;
}
// The checkpoint is generated according to ISO 7064: 1983.MOD 11-2. X can be considered as a number 10.
// Analyze the birthdate and checkpoint 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 ID card number entered is incorrect! ');
Return false;
}
Else
{
// Convert the 15-digit ID card to 18-digit
// The checkpoint is generated according to ISO 7064: 1983.MOD 11-2. X can be considered as a 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 ID card number entered is incorrect! ');
Return false;
}
Else
{
// Check whether the verification code of the 18-digit ID card is correct.
// The checkpoint is generated according to ISO 7064: 1983.MOD 11-2. X can be considered as a 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 (the '18-digit ID card verification code is incorrect! Should be: '+ valnum );
Return false;
}
Return true;
}
}
Return false;
}

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.