JavaScript Verify ID card is legal example

Source: Internet
Author: User
Tags getdate

Example 1, beginner's use tutorial

The code is as follows Copy Code

function Iscardno (card)
{
The ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits, the first 17 digits, and the last is a check digit, possibly a number or a character X
var reg =/(^\d{15}$) | (^\d{18}$) | (^\d{17} (\d| X|X) $)/;
if (reg.test (card) = = False)
{
Alert ("Invalid ID card input is not valid");
return false;
}
}

Example 2, can identify the province and the identity card is true and effective

The code is as follows Copy Code
function Checkidcard (num) {
num = Num.touppercase ();
The ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits are the first 17 digits, and the last is a check digit, possibly a number or a character X.
if (!) ( /(^\d{15}$) | (^\d{17} ([0-9]| X) $)/.test (num))
{
Alert (' Enter the ID number is not the right length, or the number does not meet the requirements! \n15 digit number should be full, 18 digits can be the lowest number or X. ');
return false;
}
The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10.
The following analysis of birth date and check digit respectively
var len, re;
len = num.length;
if (len = = 15)
{
Re = new RegExp (/^ (\d{6}) (\d{2}) (\d{2}) (\d{2}) \d{3);
var arrsplit = Num.match (re);

Check if the birthday date is correct
var Dtmbirth = new Date (' ' + arrsplit[2] + '/' + arrsplit[3] + '/' + arrsplit[4] ";
var bgoodday;
Bgoodday = (dtmbirth.getyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) ;& (dtmbirth.getdate () = = number (arrsplit[4]));
if (!bgoodday)
{
Alert (' entered the ID number in the wrong birth date! ');
return false;
}
Else
{
Turn 15 ID cards into 18-bit
The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10.
var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
var ntemp = 0, I;
num = num.substr (0, 6) + ' n ' + num.substr (6, num.length-6);
for (i = 0; i < i + +)
{
Ntemp + + NUM.SUBSTR (i, 1) * arrint[i];
}
num + + arrch[ntemp% 11];
return true;
}
}
if (len = = 18)
{
Re = new RegExp (/^ (\d{6}) (\d{4}) (\d{2}) (\d{2}) (\d{3}) ([0-9]| X) $/);
var arrsplit = Num.match (re);

Check if the birthday date is correct
var Dtmbirth = new Date (arrsplit[2] + "/" + arrsplit[3] + "/" + arrsplit[4]);
var bgoodday;
Bgoodday = (dtmbirth.getfullyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) && (dtmbirth.getdate () = = number (arrsplit[4]);
if (!bgoodday)
{
Alert (' entered the ID number in the wrong birth date! ');
return false;
}
Else
{
Verify that the check code for the 18-bit ID card is correct.
The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10.
var valnum;
var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
var ntemp = 0, I;
for (i = 0; i < i + +)
{
Ntemp + + NUM.SUBSTR (i, 1) * arrint[i];
}
Valnum = arrch[ntemp% 11];
if (Valnum!= num.substr (17, 1))
{
Alert (' 18-bit ID card is not the correct check code! should be: ' + valnum ';
return false;
}
return true;
}
}
return false;
}
Related Article

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.