ID card Verification is the simplest way to judge the length of the, of course, this very simple broken if you want to more regular we need to do some of the ID number according to the build rules to verify that some of the following functions we take a look.
Example, today share a PHP verification ID number is the correct function.
/********************php Verify that the ID number is correct *********************/function 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 = "a". $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 that the birthday date is correct {return FALSE;
else {//verify that 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. $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;
}
}
}
}
Call ID authentication function
$IDC =is_idcard ("ID card 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) {Retu
RN false;
}//Weighted factor $factor =array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
Check code corresponding to the value $verify _number_list=array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');
$checksum = 0;
For ($i =0 $i <strlen ($idcard _base), $i + +) {$checksum + + substr ($idcard _base, $i, 1) * $factor [$i];
$mod = $checksum% 11;
$verify _number= $verify _number_list[$mod];
return $verify _number;
//Upgrade 15-bit ID to 18-bit function idcard_15to18 ($idcard) {if (strlen ($idcard)!=15) {return false; }else{//If the ID card sequence code is 996 997 998 999, these are special codes for centenarians or older people if (Array_search substr ($idcard, 12,3), Array (' 996 ', ' 997 ', ' 998 ', ') 999 ')!== false) {$idcard =substr ($idcard,0,6). '
substr ($idcard, 6,9); }else{$idcard =substr ($idcard, 0,6).
substr ($idcard, 6,9);
}} $idcard = $idcard. Idcard_verify_number ($idcard);
return $idcard;
///18 ID Check Code validation function Idcard_checksum18 ($idcard) {if (strlen ($idcard)!=18) {return false;
$idcard _base=substr ($idcard, 0,17);
if (Idcard_verify_number ($idcard _base)!=strtoupper (substr ($idcard, 17,1)) {return false;
}else{return true;
}
}
Call methods such as:Validation_filter_id_card (' identity card number ');
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.