The simple example _javascript skill of JS to realize ID card number verification

Source: Internet
Author: User
Tags getdate

The following is based on the ID number coding rules, using JS to validate the code

The Idcard-validate.js code is as follows:

Copy Code code as follows:

/**
* ID Card 15-bit coding rule: dddddd YYMMDD xx p
* DDDDDD: Area code
* YYMMDD: Date of birth
* XX: Sequential class code, cannot be determined
* p: Sex, odd for male, even for female
* <p/>
* ID card 18-bit coding rule: dddddd yyyymmdd xxx y
* DDDDDD: Area code
* YYYYMMDD: Date of birth
* XXX: Sequence class Code, cannot be determined, odd number is male, even number is female
* Y: Check code, which can be obtained by the first 17 digits
* <p/>
* 18-bit number weighting factor (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 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), 11)
* I for the ID card number from the right to the left of the 2...18 bit; Y_p for the Foot check code is the location of the check Code array
*
*/

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 ("");//Get ID card array
if (Isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {
return true;
}else {
return false;
}
} else {
return false;
}
}
/**
* Determine whether the final verification bit is correct when the ID number is 18 digits
* @param a_idcard ID number array
* @return
*/
function Istruevalidatecodeby18idcard (a_idcard) {
var sum = 0; Declaring a weighted sum variable
if (a_idcard[17].tolowercase () = = ' X ') {
A_IDCARD[17] = 10;//replaces 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 verification code
if (a_idcard[17] = = Validecode[valcodeposition]) {
return true;
} else {
return false;
}
}
/**
* Judging by ID card is male or female
* @param idcard 15/18-digit ID number
* @return ' female '-female, ' male '-male
*/
function Maleorfemalbyidcard (idcard) {
Idcard = Trim (idcard.replace (//g, ""))//To handle the ID 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;
}
Can be processed directly as an array for incoming characters
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 that the birthdays in the 18-digit ID number are valid birthdays
* @param idcard 18-digit 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,parsefloat (month) -1,parsefloat (day));
We use getFullYear () to get the year to avoid the millennium bug problem.
if (Temp_date.getfullyear ()!=parsefloat (year)
|| Temp_date.getmonth ()!=parsefloat (month)-1
|| Temp_date.getdate ()!=parsefloat (day)) {
return false;
}else{
return true;
}
}
/**
* Verify that the birthdays in the 15-digit ID number are valid birthdays
* @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));
The GetYear () method is used for your age in the old ID card without considering the millennium bug problem.
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, "");
}


For the above code, the sex of the judgement in the actual use, can be added first to determine whether the identity card is valid. In this code example does not carry out this kind of judgment, a little chicken feeling, can in actual use according to the situation to plump.

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.