身份证校验-----后端

匿名 (未验证) 提交于 2019-12-02 22:11:45

用的是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(); }

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!