thinkphp表單驗證

thinkphp框架提供了表單驗證功能,分爲靜態驗證和動態驗證。php

1、靜態驗證:html

一、首先咱們在Admin的IndexController.class.php下寫一個方法yanzheng,而後用create方法對錶單提交進行驗證:jquery

namespace Admin\Controller;
use Home\Controller\BaseController;
class IndexController extends BaseController{
    Public function yanzheng(){
        $u = D("users");
    if(empty($_POST)){
        $this->show();
    }
    else{
         if($u->create()){
            echo "驗證經過";
            }
            else{
                echo $u->getError();//獲取失敗
                }                    
            }
        }        

二、在view頁面建立一個yanzheng,html頁面:ajax

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="__ACTION__" method="post">
        <div> 用戶名:<input type="text" name="uid"></div>
        <div> 請輸入密碼:<input type="text" name="pwd1"></div>
        <div> 請再次輸入密碼:<input type="text" name="pwd2"></div>
        <div> 年齡:<input type="text" name="age"></div>
        <div> 郵箱:<input type="text" name="email"></div>
        <input type="submit" value="驗證" />
        </form>
    </body>
</html>

效果圖:thinkphp

 

咱們輸入一個用戶名來驗證:php框架

密碼不一致時:框架

2、動態驗證:post

(1)  YanzhengController.class.php頁面ui

<?php
namespace Ceshi\Controller;
use Think\Controller;
class YanzhengController extends  Controller
{
     public function dtyz(){
             
             
            if(empty($_POST)){
                $this->show();
            }
            else
            {  
                //驗證規則
                $rules = array(   
                array('uid','require','用戶名不能爲空!'),
                 );
                      
                 $u= M("users");
                                           
                  if($u->validate($rules)->create()){
                    $this->ajaxReturn("OK","eval"); //若是成功
                  }else{
                    $this->ajaxReturn($u->getError(),"eval");  //若是不成功
                  }
                     
            }
        }
  public function _empty(){
    echo "您訪問的操做方法不存在!";
   }
}        

(2) dtyz.html頁面:this

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <!--引入js-->
        <script src="__ROOT__/Public/js/jquery-1.11.2.min.js"></script>
    </head>
    <body>
        <form action="__ACTION__" method="post">
        <div> 用戶名:<input type="text" name="uid" id="uid"><span id="ts"></span></div>         
        <input type="submit" value="驗證" />
        </form>
    </body>
    <script>
        //文本框失去焦點時,顯示提示信息
        $("#uid").blur(function(){
            var uid = $(this).val();
            $.ajax({
                url:"__ACTION__",
                data:{uid:uid},
                type:"POST",
                dataType:"TEXT",
                success:function(data){
                    alert(data);
                     if(data.trim()=="OK")
                     {
                        $("#ts").html("驗證經過!");
                     }else{
                        $("#ts").html("用戶名不能爲空!");
                     }
                }
            });
        })
    </script>
</html>

相關文章
相關標籤/搜索