Browse Source

hongkong phone number

master
kuangweijian 3 years ago
parent
commit
b6f77a61b1
  1. 2
      examples/user-register.php
  2. 4
      src/models/UserInput.php
  3. 43
      src/validators/HKPhoneNum.php
  4. 29
      src/validators/HKPhoneNumValidator.php

2
examples/user-register.php

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

4
src/models/UserInput.php

@ -3,7 +3,7 @@
use yii\base\Model;
use wiggins\myProject\validators\MyValidator;
use wiggins\myProject\validators\HKPhoneNumValidator;
use wiggins\myProject\validators\HKPhoneNum;
/**
@ -86,7 +86,7 @@
[['name','email','hkPhoneNum','password','pwdConfirm'],'required','on'=>'register'],
[['name','password'],'required','on'=>'login'],
['name','wiggins\myProject\validators\MyValidator'],
['hkPhoneNum','wiggins\myProject\validators\HKPhoneNumValidator'],
['hkPhoneNum','wiggins\myProject\validators\HKPhoneNum'],
];
}
}

43
src/validators/HKPhoneNum.php

@ -0,0 +1,43 @@
<?php
namespace wiggins\myProject\validators;
use yii\validators\Validator;
use Yii;
/**
* 验证电话号码是否香港号码格式
* @author kuangweijian <867034892@qq.com>
* @since 1.0.0
*/
class HKPhoneNum extends Validator
{
/**
* @var string 验证香港电话号码的正则表达式
*/
public $pattern = '/^([6|9])\d{7}$/';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute}:{value} is not a valid hongkong phone number.');
}
}
/**
* {@inheritdoc}
*/
public function validateAttribute($model, $attribute)
{
echo "test";
if(!preg_match($this->pattern, $model->$attribute))
{
return false;
}
return true;
}
}
?>

29
src/validators/HKPhoneNumValidator.php

@ -1,29 +0,0 @@
<?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;
}
}
?>
Loading…
Cancel
Save