Verify the validity of the js id card number

Source: Internet
Author: User
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

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.