ID card authenticity Check JS, ID card length regular verification

Source: Internet
Author: User

Authenticity check of ID card number

<input type= "text" value= "Please enter your ID number" id= "cards" >
<button onclick= "Isidcardno (CV)" >ok</button>

<script>


function $ (ID) {return document.getElementById (ID);}
var CV; Gets the value of the input box
$ (' cards '). onblur = function () {
Console.log ($ (' cards '). Value)
CV = $ (' cards '). Value;
}

//This can verify 15-bit and 18-bit IDs, and includes validation of birthdays and check bits.
//If interested, you can also add the identification of the location of the identity card, that is, the first 6 digits some numbers are legal.
function Isidcardno (num) {
num = Num.touppercase ();
The ID number is 15-bit or 18-bit, 15-bit is all digits, 18 bits is the first 17 digits, and the last one is the check digit, possibly the number or the character X.
if (! ( /(^\d{15}$) | (^\d{17} ([0-9]| X) $)/.test (num))) {
Alert (' Enter the wrong ID number, or the number does not meet the rules! \n15 digits should be all numbers, the 18-digit number can be the number or X. ‘);
return false;
}
//Check digit is generated according to ISO 7064:1983.mod 11-2, X can be considered as the number 10.
//below analyze birth date and check digit separately
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 that 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 in the ID number of the birth date is wrong! ‘);
return false;
} else {
//Convert 15-bit ID to 18-bit
//Check digit is generated according to ISO 7064:1983.mod 11-2, X can be considered as 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) + ' + ' + num.substr (6, num.length-6);
for (i = 0; i <; i++) {
Ntemp + = Num.substr (i, 1) * arrint[i];
}
num + = arrch[ntemp% 11];
return num;
}
}
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 that 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 (Dtmbirth.getyear ());
Alert (arrsplit[2]);
Alert (' entered in the ID number of the birth date is wrong! ‘);
return false;
} else {
//Verify that the check code for the 18-digit ID is correct.
//Check digit is generated according to ISO 7064:1983.mod 11-2, X can be considered as 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 ID ' check code is incorrect!) should be: ' + valnum);
return false;
}
return num;
}
}
return false;
}
</script>

Simple regular validation of the ID card length (the following takes effect under the MINIUI framework)

function Onidcardsvalidation (e) {
if (e.isvalid) {
var pattern =/(^\d{15}$) | (^\d{18}$) | (^\d{17} (\d| X|X) $)/;
if (E.value.length < | | e.value.length > | | pattern.test (e.value) = = False) {
E.errortext = "ID card input is not legal";
E.isvalid = false;
}
};
}

ID card authenticity Check JS, ID card length regular verification

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.