ID Card Verification The simplest is the length of the judgment, of course, this is very simple to break if you want more rules we need to do some of the ID card number according to the generated rules to verify, the following sorting out some of the functions we have to look at.
Example, share today a PHP Verified ID card number is the correct function.
/********************php Verify that the ID number is correct function *********************/functions Is_idcard ($id) {$id = Strtoupper ($id); $REGX = "/(^\d{15}$) | (^\d{17} ([0-9]| X) $)/"; $arr _split = Array (); if (!preg_match ($REGX, $id)) {return FALSE; } if (15==strlen ($id))//Check 15-bit {$regx = "/^ (\d{6}) + (\d{2}) + (\d{2}) + (\d{2}) + (\d{3}) $/"; @preg_match ($REGX, $id, $arr _split); Check that the birthday date is correct $dtm _birth = "+". $arr _split[2]. '/' . $arr _split[3]. '/'. $arr _split[4]; if (!strtotime ($dtm _birth)) {return FALSE; } else {return TRUE; }} else//check 18-bit {$regx = "/^ (\d{6}) + (\d{4}) + (\d{2}) + (\d{2}) + (\d{3}) ([0-9]| X) $/"; @preg_match ($REGX, $id, $arr _split); $dtm _birth = $arr _split[2]. '/' . $arr _split[3]. '/'. $arr _split[4]; if (!strtotime ($dtm _birth))//Check whether the birthday date is correct {return FALSE; } else {//verify that the check code for the 18-bit ID is correct. The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered the number 10. $arr _int = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3,7, 9, 10, 5, 8, 4, 2); $arr _ch = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); $sign = 0; for ($i = 0; $i < $i + +) {$b = (int) $id {$i}; $w = $arr _int[$i]; $sign + = $b * $W; } $n = $sign% 11; $val _num = $arr _ch[$n]; if ($val _num! = substr ($id, 1)) {return FALSE; }//phpfensi.com else {return TRUE; } } } }
Calling the identity card validation function
$IDC =is_idcard ("identification number");
if ($IDC) {echo "correct";} Else{echo "Error";}
Example two:
function Validation_filter_id_card ($id _card) {if (strlen ($id _card) ==18) {return idcard_checksum18 ($id _card); }elseif ((strlen ($id _card) ==15) {$id _card=idcard_15to18 ($id _card); Return Idcard_checksum18 ($id _card); }else{return false; }}//Calculate ID Check code, according to national standard GB 11643-1999 function Idcard_verify_number ($idcard _base) {if (strlen ($idcard _base)!=17) {return F Alse; }//Weighted factor $factor =array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2); Parity codes correspond to values $verify _number_list=array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); $checksum = 0; for ($i =0; $i
Call method such as: validation_filter_id_card (' ID card number ');
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.