1.验证器代码
<?php namespace app\user\validate; use think\Validate;
use Potting\IDCard; /**
* 山区治理报名验证器
* Class ActivityEnroll
* @table qj_activity_enroll 验证器类名基于数据表名命名
* @package app\user\validate
*/
class ActivityEnroll extends Validate
{
protected $rule = [
['username', 'require|max:30'],
['mobile', 'require|checkMobile'],
['sex', 'require|number'],
['email', 'require|email|max:50'],
['birthday', 'require|date'],
['id_card', 'require|checkIDCard'],
['signature', 'require|max:50'],
['if_graduates', 'require'],
['if_volunteer', 'require'],
['service_city', 'require'],
['if_adjustment', 'require'],
['areaid', 'require'],
['salaryid', 'require'],
['tradeid', 'require'],
['positionname', 'require'],
['educationid', 'require|number'],
['schoolname', 'require|max:40'],
['major', 'require|max:40'],
['school_begintime', 'require|date'],
['school_endtime', 'require|date'],
['organic_name', 'require'],
['position_award', 'require'],
['inschool_begintime', 'require'],
['inschool_endtime', 'require'],
['inschool_description', 'require'],
['type', 'require'],
['skill_name', 'require'],
['description', 'require'], ]; protected $field = [
'username' => '姓名',
'mobile' => '手机号码',
'sex' => '性别',
'email' => '邮箱',
'birthday' => '生日',
'id_crard' => '身份证',
'signature' => '座右铭',
'if_graduates' => '是否应届毕业生',
'if_volunteer' => '是否有志愿经历',
'service_city' => '可接受服务城市',
'if_adjustment' => '是否服从调剂',
'areaid' => '职业规划-意向城市',
'salaryid' => '职业规划-薪资范围',
'tradeid' => '职业规划-意向行业',
'positionname' => '职业规划-意向职位',
'educationid' => '教育经历-学历',
'schoolname' => '教育经历-学校名称',
'major' => '教育经历-专业',
'school_begintime' => '教育经历-入学时间',
'school_endtime' => '教育经历-毕业时间',
'organic_name' => '在校经历-组织名称',
'position_award' => '在校经历-担任职务',
'inschool_begintime' => '在校经历-加入时间',
'inschool_endtime' => '在校经历-结束时间',
'inschool_description' => '在校经历-工作内容',
'type' => '技能爱好类型',
'skill_name' => '技能名称',
'description' => '自我评价', ]; /**
* 检查身份证格式
* @param $value|验证数据
* @return bool|string
*/
protected function checkIDCard($value){
$IDCard = new IDCard();
$result = $IDCard::isCard($value);
if(!$result){
return '身份证号码格式错误';
}
return true; } /**
* 检查手机号码格式
* @param $value |验证数据
* @param $rule |验证规则
* @param $data |全部数据
* @return bool|string
*/
protected function checkMobile($value)
{
$isMobile = is_mobile('+86'.$value);
if (!$isMobile)
return '手机号码格式错误';
return true;
} }
2.第三方类库(验证身份证号码)
<?php namespace Potting;
/**
* 身份证处理类
*/
class IDCard { //检证身份证是否正确
public static function isCard($card) {
$card = self::to18Card($card);
if (strlen($card) != 18) {
return false;
} $cardBase = substr($card, 0, 17); return (self::getVerifyNum($cardBase) == strtoupper(substr($card, 17, 1)));
} //格式化15位身份证号码为18位
public static function to18Card($card) {
$card = trim($card); if (strlen($card) == 18) {
return $card;
} if (strlen($card) != 15) {
return false;
} // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
if (array_search(substr($card, 12, 3), array('996', '997', '998', '999')) !== false) {
$card = substr($card, 0, 6) . '18' . substr($card, 6, 9);
} else {
$card = substr($card, 0, 6) . '19' . substr($card, 6, 9);
}
$card = $card . self::getVerifyNum($card);
return $card;
} // 计算身份证校验码,根据国家标准gb 11643-1999
private static function getVerifyNum($cardBase) {
if (strlen($cardBase) != 17) {
return false;
}
// 加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // 校验码对应值
$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $checksum = 0;
for ($i = 0; $i < strlen($cardBase); $i++) {
$checksum += substr($cardBase, $i, 1) * $factor[$i];
} $mod = $checksum % 11;
$verify_number = $verify_number_list[$mod]; return $verify_number;
}
} ?>
3.公共函数common.php代码部分(验证手机号码格式)
//检测是否为手机号码
function is_mobile($mobile)
{
try {
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse($mobile, "CH");
$isValid = $phoneUtil->isValidNumber($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
return $e;
}
return $isValid;
}