Browse Source

香港电话格式验证

master
kuangweijian 3 years ago
parent
commit
aa0fc4ff93
  1. 1
      examples/user-register.php
  2. 13
      src/models/UserInput.php
  3. 29
      src/validators/HKPhoneNumValidator.php
  4. 2
      src/validators/MyValidator.php

1
examples/user-register.php

@ -10,6 +10,7 @@
$inputDate=[ $inputDate=[
"name" => "kuangweijian", "name" => "kuangweijian",
"email" => "12345678@qq.com", "email" => "12345678@qq.com",
"hkPhoneNum"=> "81234567",
"password" => "1234567890", "password" => "1234567890",
"pwdConfirm"=> "1234567890", "pwdConfirm"=> "1234567890",
]; ];

13
src/models/UserInput.php

@ -3,6 +3,8 @@
use yii\base\Model; use yii\base\Model;
use wiggins\myProject\validators\MyValidator; use wiggins\myProject\validators\MyValidator;
use wiggins\myProject\validators\HKPhoneNumValidator;
/** /**
* UserInput类是用于用户登录或注册场景中输入数据 * UserInput类是用于用户登录或注册场景中输入数据
@ -21,6 +23,11 @@
*/ */
public $email; public $email;
/**
* 香港电话号码
*/
public $hkPhoneNum;
/** /**
* @var string 密码 * @var string 密码
*/ */
@ -49,6 +56,7 @@
return [ return [
'name' => '3-20个英文或数字字符', 'name' => '3-20个英文或数字字符',
'email' => '你的邮箱地址', 'email' => '你的邮箱地址',
'hkPhoneNum'=> '香港电话号码',
'password' => '3-10个英文或数字字符', 'password' => '3-10个英文或数字字符',
'pwdConfirm' => '需要与密码一致', 'pwdConfirm' => '需要与密码一致',
]; ];
@ -61,7 +69,7 @@
{ {
return[ return[
self::SCENARIO_LOGIN => ['name','password'], self::SCENARIO_LOGIN => ['name','password'],
self::SCENARIO_REGISTER => ['name','email','password','pwdConfirm'],
self::SCENARIO_REGISTER => ['name','email','hkPhoneNum','password','pwdConfirm'],
]; ];
} }
@ -75,9 +83,10 @@
['email','email'], ['email','email'],
['password','string','length'=>[3,10]], ['password','string','length'=>[3,10]],
['pwdConfirm','compare','compareAttribute'=>'password','message'=>'两次密码必须一致'], ['pwdConfirm','compare','compareAttribute'=>'password','message'=>'两次密码必须一致'],
[['name','email','password','pwdConfirm'],'required','on'=>'register'],
[['name','email','hkPhoneNum','password','pwdConfirm'],'required','on'=>'register'],
[['name','password'],'required','on'=>'login'], [['name','password'],'required','on'=>'login'],
['name','wiggins\myProject\validators\MyValidator'], ['name','wiggins\myProject\validators\MyValidator'],
['hkPhoneNum','wiggins\myProject\validators\HKPhoneNumValidator'],
]; ];
} }
} }

29
src/validators/HKPhoneNumValidator.php

@ -0,0 +1,29 @@
<?php
namespace wiggins\myProject\validators;
use yii\validators\Validator;
/**
* 验证电话号码是否香港号码格式
* @author kuangweijian <867034892@qq.com>
* @since 1.0.0
*/
class HKPhoneNumValidator extends Validator
{
/**
* 对指定的属性进行验证
* @param yii\base\Model $model 被校验的数据模型
* @param string $attribute 被校验的属性名称
* @return bool 验证成功返回true,验证失败返回false
*/
public function validateAttribute($model, $attribute) : bool
{
if(!preg_match("/^([6|9])\d{7}$/", $model->$attribute))
{
$this->addError($model, $attribute,"{$model->$attribute}不属香港号码");
return false;
}
return true;
}
}
?>

2
src/validators/MyValidator.php

@ -2,6 +2,7 @@
namespace wiggins\myProject\validators; namespace wiggins\myProject\validators;
use yii\validators\Validator; use yii\validators\Validator;
/** /**
* MyValidator类用于独立验证器 * MyValidator类用于独立验证器
@ -12,7 +13,6 @@
{ {
/** /**
* 对指定的属性进行验证 * 对指定的属性进行验证
*
* @param yii\base\Model $model 被校验的数据模型 * @param yii\base\Model $model 被校验的数据模型
* @param string $attribute 被校验的属性名称 * @param string $attribute 被校验的属性名称
* @return bool 验证成功返回true,验证失败返回false * @return bool 验证成功返回true,验证失败返回false

Loading…
Cancel
Save