TP框架---表單驗證


自動驗證是ThinkPHP模型層提供的一種數據驗證方法,可以在使用create創建數據對象的時候自動進行數據驗證。分為靜態驗證和動態驗證。

關於基礎知識,請查看手冊“自動驗證”一章。

一、靜態驗證

(1)在Home/Controller/路徑下新建Index控制器。IndexController

 IndexController.class.php頁面

注意:靜態定義方式因為必須定義模型類,所以只能用D函數實例化模型

     create方法是對表單提交的POST數據進行自動驗證

 

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
        public function yanzheng(){
		$u= D("users");//造一個子類對象
		if(empty($_POST)){
			$this->show();
		}else{
			if($u->create()){//驗證
				echo"驗證通過";
			}else{
				echo $u->getError();//獲取錯誤信息
			}
		}
	} 
}    

 (2)在view/Index文件夾下做yanzheng.html頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script>
</head>

<body>
<h1>驗證界面</h1>
<form action="__ACTION__" method="post">
<div>用戶名:<input type="text" name="uid" /></div>
<div>密碼:<input type="password" name="pwd1"/></div>
<div>確認密碼:<input type="password" name="pwd2"/></div>
<div>年齡:<input type="text" name="age"/></div>
<div>郵箱:<input type="text" name="Email"/></div>
<div><input type="submit" value="驗證" /></div>
</form>
</body>
</html>

 效果圖:

(3)在Model層寫靜態驗證的驗證:(路徑如圖)

 

UsersModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UsersModel extends Model{
		//添加驗證條件
		protected $_validate = array(     
			array("uid","require","用戶名不能為空!"), //默認情況下用正則進行驗證 
			array("pwd1","require","密碼不能為空!"),
			array("pwd2","require","密碼不能為空!"),		
			array("pwd2","pwd1","兩次輸入的密碼不一致",0,"confirm"), // 驗證確認密碼是否和密碼一致 
			array("age","18,50","年齡不在范圍內",0,"between"),
			array("Email","email","郵箱格式不正確"),
	    );
} 

 依次驗證效果圖:

當全部為空時,點擊驗證

會跳轉

輸入用戶名,其他不輸入時,會跳轉

兩次密碼輸入不一致時,會提示;年齡不在范圍內會提示;郵箱格式不正確時會提示;

輸入正確格式內容后

 二、動態驗證

(1)  IndexController.class.php頁面

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
        public function yz(){
		$u= M("users");//造一個父類對象
		if(empty($_POST)){
			$this->show();
		}else{
			
			$rules = array(
				array("uid","require","用戶名不能為空!"),
			);
			
			if($u->validate($rules)->create()){//驗證
				$this->ajaxReturn("ok","eval");
			}else{
				$this->ajaxReturn("no","eval");
			}
		}
	}  
}

 (2) yz.html頁面:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script>
</head>

<body>
<h1>驗證界面</h1>
<form action="__ACTION__" method="post">

<div><input type="text" name="uid" id="uid" /><span id="ts"></span></div>

<div><input type="submit" value="驗證" /></div>
</form>
</body>
<script type="text/javascript">
$("#uid").blur(function(){
		var uid = $(this).val();
		$.ajax({
				url:"__ACTION__",
				data:{uid:uid},
				type:"POST",
				dataType:"TEXT",
				success: function(data){
						if(data.trim()=="ok")
						{
							$("#ts").html("驗證通過");
						}
						else
						{
							$("#ts").html("用戶名不能為空");
						}
					}

				
			});
	})
</script>
</html>

 看一下效果:

 當文本框失去焦點時:

 

當文本框有內容時,再失去焦點:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM