首页 > 代码库 > PHP 验证香港身份证ID号
PHP 验证香港身份证ID号
/**
check hk idcard
$id = "K548653A";
$xx= check_hkid($id);
var_dump($xx);
**/
function check_hkid($id)
{
if (!preg_match("/^[a-zA-Z][0-9]{6}[0-9aA]$/", $id)) {
return false;
}
$mul = 8;
$sum = (ord(strtoupper($id))-64) * $mul;
while($mul>1) {
$sum += intval(substr($id, 8 - $mul, 1)) * $mul;
$mul --;
}
$chksum = dechex(strval(11-($sum % 11)));//dec to hex
if ($chksum == ‘b‘) {
$chksum = 0;
}
return $chksum == strtolower(substr($id, 7, 1));
}
JS
private function check_hkid(id) {
if (!id.match(/^[a-zA-Z][0-9]{6}[0-9aA]$/)) return false;
var mul = 8, sum = (id.toUpperCase().charCodeAt()-64) * mul--;
while (mul>1) sum += parseInt(id.substr(8-mul, 1)) * mul--;
var chksum = (11-(sum % 11)).toString(16);
if (chksum == "b") chksum = "0";
return chksum == id.substr(7,1).toLowerCase();
}
PHP 验证香港身份证ID号