JS strictly verifies the ID card number and js ID card number

Source: Internet
Author: User

JS strictly verifies the ID card number and js ID card number

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

<Script type = "text/javascript"> 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, ""); // remove the leading and trailing spaces of the string if (idCard. length = 15) {return isValidityBrithBy15IdCard (idCard); // verify the 15-digit ID card} else if (idCard. length = 18) {var a_idCard = id Card. split (""); // obtain the ID card array if (isValidityBrithBy18IdCard (idCard) & isTrueValidateCodeBy18IdCard (a_idCard )) {// perform basic verification of the 18-digit ID card and return true for the 18th-digit verification;} 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 verification code with the last digit x with 10 for subsequent operations} for (var I = 0; I <17; I ++) {sum + = Wi [I] * a_idCard [I]; // weighted sum} valCodePosition = sum % 11; // obtain the verification code location if (a_idCard [17] = ValideCode [valCodePosition]) {return true ;}else {return false ;}} /*** 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.su Bstring (); var day = idCard18.substring (); var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day )); // here, use getFullYear () to get 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 whether the birthday in the 15-digit ID card number is a valid birthday * @ param idCard15 15-digit 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); // if you are older than your ID card, you do not need to consider the millennium bug and use the getYear () method if (temp_d Ate. 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 and function trim (str) {return str. replace (/(^ \ s *) | (\ s * $)/g, "") ;}</script>


Online DemoBelieve it or not, try it.


Author: itmyhome


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.