Ultra-accurate JavaScript verification of the specific implementation of the ID number _javascript skills

Source: Internet
Author: User
Tags getdate

Before writing a program, you should understand the composition of the ID number:

The ID card number is divided into two, the old one is 15 digits, the new one is 18 digits.

ID 15-bit encoding rule: dddddd YYMMDD xx p

Where DDDDDD: Region code yymmdd: birth date XX: Sequence class code, cannot determine p: sex, odd for male, even for female;

ID 18-bit encoding rule: dddddd yyyymmdd xxx y

Among them dddddd: region code YYYYMMDD: birth date XXX: Sequence class code, cannot determine, odd number is male, even is female y: Check code, this bit value can be obtained by the first 17 digits calculation, the formula of calculation see program, Some constants need to use :

The 18-digit number weighting factor is (from right to left) Wi = [7, 9, 5, 8, 4, 2 , 1, 6, 3, 7, 9, ten, 5, 8, 4, 2,1] Verify bit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2] check digit calculation formula:y_p = mod (∑ (AIXWI), one)

I for the ID card number from the right to the left number of 2...18 bit; Y_p is the location of the check code array for the foot check code.

var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];   Weighted 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 string and tail space if (idcard.length = =) {return Isvaliditybrithby15idcard (Idcard);    Verification of 15-bit IDs} else if (Idcard.length =) {var A_idcard = Idcard.split (""); Get ID card array if (Isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {// 
  Basic authentication for 18-bit IDs and 18th-bit validation return true; 
  }else {return false; 
 } else {return false; }/** * Determines whether the final verification bit is correct when the ID number is 18 * @param a_idcard ID Number array * @return/function Istruevalidatecodeby18idcard (a        _idcard) {var sum = 0;     Declares a weighted sum variable if (a_idcard[17].tolowercase () = = ' x ') {a_idcard[17] = 10;   Replace the last bit X's captcha with 10 for subsequent operations} for (var i = 0; i < i++) {sum + = wi[i] * A_idcard[i];    Weighted sum} valcodeposition = sum% 11;Get the location of the captcha if (a_idcard[17] = = Validecode[valcodeposition]) {return true; 
 else {return false; }/** * Verify that the birthday in the 18-digit ID number is a valid birthday * @param idcard 18-digit ID card String * @return/function Isvaliditybrithby18idcard (IdC 
 ARD18) {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)); Use getFullYear () to get the year to avoid the bug problem if (Temp_date.getfullyear ()!=parsefloat () | | 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 number is a valid birthday * @param idCard15 15-digit 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)); You don't need to consider the Millennium bug question about your age in your old ID card.Use the GetYear () method if (temp_date.getyear)!=parsefloat (year) | | Temp_date.getmonth ()!=parsefloat (month)-1 | | 
  Temp_date.getdate ()!=parsefloat (Day)} {return false; 
  }else{return true; }///Remove string-and-tail space function trim (str) {return str.replace (/^\s*) | ( 
\s*$)/g, "");  }

A gender determination based on the ID number

/** 
 * Through the identity card is a male or female 
 * @param idcard 15/18 ID card number 
 * @return ' female '-female, ' male '-male * * 
function Maleorfemalbyidcard (idcard) { 
 Idcard = Trim (idcard.replace (//g, ""));  Handle the ID card number. Includes 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 above is JavaScript verification ID number of the specific implementation method, and according to ID card can verify sex, is not very magical, welcome to learn from.

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.