设为首页 加入收藏

TOP

身份证校验-----15位,18位后端
2019-09-17 15:04:20 】 浏览:51
Tags:身份证 校验 -----15位 18位 后端

用的是thinkphp5 + layui

代码:

 1     //添加志愿者
 2     public function add()
 3     {
 4         if($this->request->isAjax())
 5         {
 6             $request = new Request();     
 7             $params = $this->request->param();   
 8             $title = $params['title'];
 9             if(empty($title)){
10                 $this->error('请输入志愿者名称!');
11             } 
12             $volun = $params['volun_no'];
13             if(empty($volun)){
14                 $this->error('请输入志愿者编号!');
15             }   
16             $identity = $params['identity'];
17             if(empty($identity)){
18                 $this->error('请输入您的身份证号或护照号!');
19             } 
20             $type = $params['identity_type'];
21             if(empty($identity)){
22                 $this->error('请选择您的证件类型!');
23             } 
24             //类型type=1身份证,type=2护照            
25             if(15==strlen($identity)){ //检查15位                
26                 $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
27                 $arr_split = array();
28                 @preg_match($regx, $identity, $arr_split);
29                 //检查生日日期是否正确
30                 $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
31                 if(!strtotime($dtm_birth))
32                 {
33                     $this->error('您输入的身份证号码有误!');
34                 } 
35                 $result = Db::name('volunteers')->where(['identity'=>$identity])->find();
36                 if($result){
37                     $this->error('该志愿者身份证号已存在,请勿重复添加!');
38                 }else{
39                     $data = [
40                         'title'          => isset($params['title']) ? $params['title'] : '',
41                         'volun_no'       => isset($params['volun_no']) ? $params['volun_no'] : '',
42                         'userid'         => isset($params['userid']) ? $params['userid'] : 0,
43                         'createtime'     => time(),
44                         'updatetime'     => time(),
45                         'identity'       => $params['identity'],
46                         'identity_type'  => isset($params['identity_type']) ? $params['identity_type'] : 1,
47                     ]; 
48                     if($result = Db::name('volunteers')->insert($data))
49                     {
50                         return $this->success('志愿者添加成功!');
51                     }else{
52                         return $this->error('志愿者添加失败!');
53                     } 
54                     return;
55                 }  
56             }
57             if( strlen($identity ) != 18) {
58                 $this->error('您输入的身份证号码有误!');
59             }
60             $crad_id_base = substr($identity , 0, 17);
61             $crad_id_verify = substr($identity , -1);
62             // 加权因子 
63             $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3);
64             // 校验码对应值 
65             $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
66             $checksum = 0;
67             for( $i = 0; $i < strlen($crad_id_base); $i++ ) {
68                 $checksum += substr($crad_id_base, $i, 1) * $factor[$i % 10];
69             }
70             $mod = $checksum % 11;
71             if( $crad_id_verify == $verify_number_list[$mod] ) {
72                 $result = Db::name('volunteers')->where(['identity'=>$identity])->find();
73                 if($result){
74                     $this->error('该志愿者身份证号已存在,请勿重复添加!');
75                 }else{
76                     $data = [
77                         'title'          => isset($params['title']) ? $params['title'] : '',
78                         'volun_no'       => isset($params['volun_no']) ? $params['volun_no'] : '',
79                         'userid'         => isset($params['userid']) ? $params['userid'] : 0,
80                         'createtime'     => time(),
81                         'updatetime'     => time(),
82                         'identity'       => $params['identity'],
83                         'identity_type'  => isset($params['identity_type']) ? $params['identity_type'] : 1,
84                     ]; 
85                     if($result = Db::name('volunteers')->insert($data))
86                     {
87                         return $this->success('志愿者添加成功!');
88                     }else{
89                         return $this->error('志愿者添加失败!');
90                     } 
91                     return;
92                 }  
93                 return;
94             } 
95         }                                                    
96         return $this->fetch();
97     }

 

  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇laravel中间件的实现原理 下一篇Yii2中$model->load($data)一..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目