8.5 验证器
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <form action="{:url('check_validate')}" method="post"> <p> USERNAME:<input type="text" name="username" id="" value="" /> </p> <p> PASSWORD:<input type="password" name="password" id="" value="" /> </p> <p> REPASSWORD:<input type="password" name="repassword" id="" value="" /> </p> <p> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </p> </form> </body> </html><?php ?>
<?php namespace app\index\controller; use think\Controller; use think\Validate;//使用验证器 class Yanzheng extends Controller{ public function test(){ return view(); } public function check_validate(){ $data = input("post."); //Validate([验证规则],[自定义错误信息]) $validate = new Validate([ "username"=>"require|length:6,12", "password"=>"require|confirm:repassword"], ["username.require"=>"名称必须", "password.require"=>"长度不对" ]); if($validate->check($data)){ }else{ dump($validate->getError()); } } //普通判断 public function check(){ //dump(request()); $data = input('post.');//获取数据 if($data['username']){ //判断用户名长度 $size = strlen($data['username']); if($size>=6 && $size<=12){ if($data['password']){ if($data['password']==$data['repassword']){ $this->success("登陆成功","cheng"); }else{ $this->error("两次密码不一致"); } }else{ $this->error("密码不能为空"); } }else{ $this->error("用户名长度不对"); } }else{ $this->error("用户名不能为空"); } } public function cheng(){ echo"登陆成功"; } } ?>