php密码与重复密码相同,Yii2.0-自带注册添加重复密码和验证码

Yii2自带的注册可以作为网站的注册功能,但添加重复密码和验证码会更加完美!

问题:用户名没有做严格的限制,类似“111”,“123456”,“_____111”这样的的用户名都是被允许的,那么如何限制用户输入我们所希望的用户名呢?

一般的注册,都有重复输入密码的input框,是为了让用户再次确认自己输入的密码,如何添加呢?

为了提高注册用户的质量,防止批量注册,添加验证码是不错的选择,如何加?

如何在不修改逻辑代码的情况下完美解决以上三个问题?看了下面的教程,一目了然!

以高级版2.0.6为例,打开/frontend/models/SignupForm.phpclass SignupForm extends Model

{

public $username;

public $email;

public $password;

/**

* @inheritdoc

*/

public function rules()

{

return [

['username', 'filter', 'filter' => 'trim'],

['username', 'required'],

['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],

['username', 'string', 'min' => 2, 'max' => 255],

['email', 'filter', 'filter' => 'trim'],

['email', 'required'],

['email', 'email'],

['email', 'string', 'max' => 255],

['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

['password', 'required'],

['password', 'string', 'min' => 6],

];

}

只需修改rules规则即可完美实现

a.添加用户字符限制,6-16位['username', 'string', 'min' => 6, 'max' => 16],

输入限制:用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。['username', 'match','pattern'=>'/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u','message'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],

b.添加重复密码字段public $repassword;

一般重复密码与密码的字段验证基本上是一致的,所以可以在password中添加repassword,并添加两次输入一致的限制[['password','repassword'], 'required'],

[['password','repassword'], 'string', 'min' => 6],

['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],

c.添加验证码字段public $verifyCode;

验证码有自带的扩展,只需添加以下代码即可['verifyCode', 'captcha'],

注意:需要在对应的控制器中添加以下代码,本例为SiteController中添加public function actions()

{

return [

'captcha' => [

'class' => 'yii\captcha\CaptchaAction',

'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,

],

];

}

修改之后的规则class SignupForm extends Model

{

public $username;

public $email;

public $password;

public $repassword;

public $verifyCode;

public function rules()

{

return [

['username', 'filter', 'filter' => 'trim'],

['username', 'required'],

['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '该用户名已被使用!'],

['username', 'string', 'min' => 6, 'max' => 16],

['username', 'match','pattern'=>'/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u','message'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],

['email', 'filter', 'filter' => 'trim'],

['email', 'required'],

['email', 'email'],

['email', 'string', 'max' => 255],

['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '该邮箱已经被注册!'],

[['password','repassword'], 'required'],

[['password','repassword'], 'string', 'min' => 6],

['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],

['verifyCode', 'captcha'],

];

}

....

验证一下效果:

9de006299c870d67c2e341f94eebcab5.png

酱油出品,必是精品!O(∩_∩)O~,未经本站授权,禁止私自转载!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值