Simple Example of implementing ID card number verification in js

Source: Internet
Author: User

The following code verifies the validity of an ID card number based on the ID card number encoding rules using JS.

The IdCard-Validate.js code is as follows:
Copy codeThe 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.
* <P/>
* 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.
* <P/>
* 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 ,"");
}

In the above code, you can add the first check to determine whether the ID card is valid in actual use. This kind of judgment is not carried out in this sample code, and it is a little tasteless. It can be fully utilized in actual use.

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.