JavaScript ID Verification Regular and detailed description

Source: Internet
Author: User
Tags getdate regular expression

Webpage Special effect ID card verification regular and detailed explanation
1, a simple regular expression:

(1) preg_match ("/^ (d{18,18}|d{15,15}|d{17,17}x) $/", $id _card)
(2) Preg_match ("/^ (D{6})" (18|19|20)? D{2}) ([01]d) ([0123]d) (D{3}) (d|x) $/", $id _card)
(3) Preg_match ("/(^d{15}$/) | ( D{17} (?:d |x|x) $/), $id _card)

2. Complex and rigorous verification:

This verifies 15-bit and 18-bit IDs and includes validation of birthdays and check-bits.
If you are interested, you can also add the identity card location of the verification, that is, the first 6 digits of some of the legal number is illegal.

Function Isidcardno (num)
{  
          num = Num.touppercase (); 
        //ID number is 15 digits or 18 digits, 15 digits are all digits, 18 Digits, the first 17 digits, and the last one is a check digit, possibly a number or a character X.   
          if (! /(^d{15}$) | (^d{17} ([0-9]|x) $)/.test (num))   
          {
               alert (' Input ID number is not the right length, Or the number doesn't match the rules! 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 the number 10.
//below analyze birth date and check digit respectively
Var len, re;
Len = num.length;
if (len = =)
{
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 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 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 (Dtmbirth.getyear ());
Alert (arrsplit[2]);
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 num;
}
}
return false;
}
3, the strict verification:

<script>
var acity={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: " Jiangsu ", 33:" Zhejiang ", 34:" Anhui ", 35:" Fujian ", 36:" Jiangxi ", 37:" Shandong ", 41:" Henan ", 42:" Hubei ", 43:" Hunan ", 44:" Guangdong ", 45:" Guangxi ", 46:" Hainan ", 50:" Chongqing ", 51:" Sichuan ", 52 : "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"}

function Cidinfo (SID) {
var isum=0
var info= ""
if (!/^d{17} (d|x) $/i.test (SID)) return false;
Sid=sid.replace (/x$/i, "a");
if (Acity[parseint (Sid.substr (0,2))]==null) return "Error: illegal area";
Sbirthday=sid.substr (6,4) + "-" +number (Sid.substr (10,2)) + "-" +number (Sid.substr (12,2));
var d=new date (Sbirthday.replace (/-/g, "/")
if (sbirthday!= (d.getfullyear () + "-" + (D.getmonth () +1) + "-" + d.getdate ()) return "Error: illegal birthday";
for (var i = 17;i>=0;i-) Isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), 11)
if (isum%11!=1) return "error: illegal license number";
Return Acity[parseint (Sid.substr (0,2))]+ ", +sbirthday+", "+ (SID.SUBSTR (16,1)%2?" Male ":" female ")
}
document.write (Cidinfo ("380524198002300016"), "<br/>");
document.write (Cidinfo ("340524198002300019"), "<br/>")
document.write (Cidinfo ("340524197711111111"), "<br/>")
document.write (Cidinfo ("34052419800101001x"), "<br/>");
</script>


It's on the game website.

<script language= "javascript" type= "Text/javascript" >
function Checkidcardfield (v_card) {
var reg =/^d{15} (d{2}[0-9x])? $/i;
if (!reg.test (V_card)) {
return false;
}
if (v_card.length = = 15) {
var n = new Date ();
var y = n.getfullyear ();
if (parseint ("+ V_CARD.SUBSTR (6, 2)) < 1900 | | parseint (" + V_CARD.SUBSTR (6, 2)) > y) {
return false;
}
var birth = "n" + v_card.substr (6, 2) + "-" + v_card.substr (8, 2) + "-" + v_card.substr (10, 2);
if (!isdate (birth)) {
return false;
}
}
if (v_card.length = = 18) {
var n = new Date ();
var y = n.getfullyear ();
if (parseint (V_card.substr (6, 4)) < 1900 | | parseint (V_CARD.SUBSTR (6, 4)) > y) {
return false;
}
var birth = v_card.substr (6, 4) + "-" + v_card.substr (2) + "-" + v_card.substr (12, 2);
if (!isdate (birth)) {
return false;
}
IW = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
isum = 0;
for (i = 0; i < i++) {
IC = V_card.charat (i);
Ival = parseint (IC);
Isum + + ival * Iw[i];
}
Ijym = isum% 11;
if (Ijym = = 0) Sjym = "1";
else if (Ijym = 1) Sjym = "0";
else if (Ijym = 2) Sjym = "X";
else if (Ijym = 3) Sjym = "9";
else if (Ijym = 4) Sjym = "8";
else if (Ijym = 5) Sjym = "7";
else if (Ijym = 6) Sjym = "6";
else if (Ijym = 7) Sjym = "5";
else if (Ijym = 8) Sjym = "4";
else if (Ijym = 9) Sjym = "3";
else if (Ijym = =) Sjym = "2";
var Ccheck = V_card.charat. toLowerCase ();
if (Ccheck!= sjym) {
return false;
}
}
return true;
}
</script>

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.