Provides various official and user-released code examples. For code reference, you are welcome to learn how to verify that the ID card number entered by the user is valid?
Most of the verification methods on the Internet only perform a simple verification of the ID card number format, and do not verify the validity of the ID card. The following function can verify the validity of the ID card number, support ID card number verification with the end of X
Function IdCardValidate (idCard ){
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
Var sum = 0;
Var isValidityBrith = function (year, month, day ){
Var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day ));
If (year. length = 2 ){
Var temp_year = temp_date.getYear ();
} Else if (year. length = 4 ){
Var temp_year = temp_date.getFullYear ();
} Else {
Return false;
}
If (temp_year! = ParseFloat (year)
| Temp_date.getMonth ()! = ParseFloat (month)-1
| Temp_date.getDate ()! = ParseFloat (day )){
Return false;
} Else {
Return true;
}
}
IdCard = idCard. replace (// g, ""). replace (/(^ \ s *) | (\ s * $)/g ,"");
If (idCard. length = 15 ){
Var year = idCard. substring (6, 8 );
Var month = idCard. substring (8, 10 );
Var day = idCard. substring (10, 12 );
Return isValidityBrith (year, month, day );
}
If (idCard. length! = 18) return false;
Var a_idCard = idCard. split ("");
If (a_idCard [17]. toLowerCase () = 'X') a_idCard [17] = 10;
For (var I = 0; I <17; I ++ ){
Sum + = Wi [I] * a_idCard [I];
}
ValCodePosition = sum % 11; // the location where the verification code is obtained
If (a_idCard [17]! = ValideCode [valCodePosition]) return false;
Var year = idCard. substring (6, 10 );
Var month = idCard. substring (10, 12 );
Var day = idCard. substring (12, 14 );
Return isValidityBrith (year, month, day );
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB