yii2表单正则验证规则

  • Maxie 发布于 2016-10-17 20:55
  • 栏目:Yii2.0
  • 1348 人围观
  • 4 个不明物体

model层书写

return

[

[['username','password','email','phone','images'],'required','message'=>'不能为空'],

//验证唯一性

[['username'], 'unique','targetClass' => '\backend\models\Verification','message'=>'用户名不能一致'],

//验证长度

['username', 'string', 'min' => 2, 'max' => 8,"tooLong"=>"太长了", "tooShort"=>"太短了"],

//正则验证

['username','match','pattern'=>'/^[a-z]{1,}$/','message'=>'用户名必须为字母'],

['password','match','pattern'=>'/^[0-9]{6,12}$/','message'=>'密码必须是数字,且大于6为小于12位'],

['email', 'unique', 'targetClass' => '\backend\models\Verification', 'message' => '邮箱名已存在.'],

//邮箱验证

['email','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/','message'=>'邮箱格式不正确'],

//手机号验证

['phone','match','pattern'=>'/^1[0-9]{10}$/','message'=>'手机号格式不正确'],

   ];


评论