身份证校验-----后端
程序员文章站
2022-07-04 23:36:02
用的是thinkphp5 + layui 代码: ......
用的是thinkphp5 + layui
代码:
public function add()
{
if($this->request->isajax())
{
$request = new request();
$params = $this->request->param();
$title = $params['title'];
$identity = $params['identity'];
if(empty($identity))
{
$this->error('请输入您的身份证号或护照号!');
}
if( strlen($identity ) != 18 )
{
$this->error('您输入的身份证号码有误!');
}
$crad_id_base = substr($identity , 0, 17);
$crad_id_verify = substr($identity , -1);
// 加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3);
// 校验码对应值
$verify_number_list = array('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2');
$checksum = 0;
for( $i = 0; $i < strlen($crad_id_base); $i++ )
{
$checksum += substr($crad_id_base, $i, 1) * $factor[$i % 10];
}
$mod = $checksum % 11;
if( $crad_id_verify == $verify_number_list[$mod] )
{
$result = db::name('volunteers')->where(['identity'=>$identity])->find();
if($result)
{
$this->error('该志愿者身份证号已存在,请勿重复添加!');
}else
{
$data = [
'title' => isset($params['title']) ? $params['title'] : '',
'volun_no' => isset($params['volun_no']) ? $params['volun_no'] : '',
'userid' => isset($params['userid']) ? $params['userid'] : 0,
'createtime' => time(),
'updatetime' => time(),
'identity' => $params['identity'],
'identity_type' => isset($params['identity_type']) ? $params['identity_type'] : 1,
];
if($result = db::name('volunteers')->insert($data))
{
return $this->success('志愿者添加成功!');
}else{
return $this->error('志愿者添加失败!');
}
return;
}
return;
}
}
return $this->fetch();
}