JavascriptforvalidateIDnumber (18digitor15digit)

Source: Internet
Author: User
* ** ID card 15-digit encoding rule: ddddddddyymmddxxp * dddddd: Location Code * yymmdd: Date of birth * xx: sequence class code, uncertain * p: Gender, odd number: male, the even number is female * & lt; p & gt; * ID card 18-bit encoding rule: ddddd/*** ID card 15-bit encoding rule: dddddddddd yymmdd xx p * dddddddd: location Code * yymmdd: Date of birth * xx: sequence class code, uncertain * p: Gender, odd number of male, even number of female *

* ID card 18-bit encoding rule: dddddddd yyyymmdd xxx y * dddddddd: Location Code * yyyymmdd: Date of birth * xxx: sequence class code, uncertain, odd number is male, even number: Female * y: Check Code. This digit 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 number of the ID card number from right to left 2... 18 bits; Y_P is the position of the checkcode array where the ankle checkcode is located **/function len (s) {var l = 0; var a = s. split (""); for (var I = 0; I if (a [I]. charCodeAt (0) <299) {l ++;} else {l + = 2 ;}} return l ;}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, ""); 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 tru E;} 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 of 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_idC Ard [17] = ValideCode [valCodePosition]) {return true;} else {return false ;}} /*** determine whether the ID card is a male or female * @ param idCard 15/18-bit ID card number * @ return 'female '-female or '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 () % 2 = 0) {return 'female ';} else {return 'male';} else if (idCard. length = 18) {if (idCard. substring () % 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, parseFloa T (month)-1, parseFloat (day); // here getFullYear () is used to get the year to avoid the problem of Millennium insects 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 ,"");}

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.