thinkphp驗證功能(部分)

來源:互聯網
上載者:User

標籤:eval   user   model   密碼   輸入   body   注釋   success   子類   

一.靜態驗證

  1.前端代碼:常規 <form> 提交表單  代碼:略

  2.後台模板代碼

  

<?phpnamespace Home\Model;use Think\Model;class userModel extends Model{protected $_validate = array(array("uid","require","使用者名稱不可為空"),array("pwd1","require","密碼不可為空"),array("pwd2","pwd1","兩次輸入的密碼不一致",0,"confirm"),array("age","18,50","年齡不在範圍內",0,"between"),array("email","email","郵箱格式不正確"),);}

 3.後台控制器代碼

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {  public function yanzheng(){$u = M("User");if(empty($_POST)){$this->show();}else{if($u->create()){//驗證echo"驗證通過";}else{echo $u->getError();//擷取錯誤資訊}}}}
//靜態驗證 1.方便:user表可直接用來進行驗證.
//          2.死板:寫死的內容,一般不去更改,改也麻煩
//         3.驗證寫在模板檔案下,都是數組方式書寫:詳見手冊
//         4.寫驗證規則須建立子類(父類無需驗證規則) D("user");
//         5.靜態驗證:不論資料庫是否由此欄位,都可以驗證
//         動態驗證:必須資料庫存在的欄位才可驗證
//         6.D("user"); 寫動態驗證是造子類對象,必須注釋模板內的驗證代碼,驗證規則寫在控制器內
       M("user");

驗證方法二擇其一,不能混合使用

二.動態驗證
  1.前端代碼:
  
<body><form action="__ACTION__" method="post"><input type="text" name="Uid" id="uid" /><span id="ts"></span><input type="submit" value="驗證" /></form></body></html><script type="text/javascript">$("#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>

   2.後台模板代碼

  

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function yz(){        $u = D("User");        if(empty($_POST)){            $this->show();        }else{            $rules = array("Uid","require","使用者名稱不可為空");                        if($u->validata($rules)->create()){                $this->ajaxReturn("OK","eval");            }else{                $this->ajaxReturn("NO","eval");            }                }    }}

 


 

thinkphp驗證功能(部分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.