1. 程式人生 > >8.5 驗證器

8.5 驗證器

() echo style pub ati 取數據 獲取數據 length ali

<!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"登陸成功"; } } ?>

8.5 驗證器