码迷,mamicode.com
首页 > Web开发 > 详细

PHP 验证香港身份证ID号

时间:2015-01-12 17:19:18      阅读:707      评论:0      收藏:0      [点我收藏+]

标签:

/**
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号

标签:

原文地址:http://www.cnblogs.com/jyb2014/p/4218730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!