thinkphp 验证

//YongHuModel.class.php
namespace Home\Model;

use Think\Model;

class YongHuModel exstends Model
{
  protected $tablePrefix ="";
  protected $trueTableName="yonghu";//真实表名
  protected $_validate=array(
  array('uid','require','用户名不能为空'),
  array('pwd','pwd1','两次输入的密码不一致!',0,'confirm'), //

个字段是否相同
        array('email','email','邮箱格式不正确'),
        array('name','/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0

-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/','身份证号不正

确!',0,'regex'),
        array('age','18,50','年龄不在范围
',0,'between'),
  );
}





//TestController.class.php

<?namespace Home\Controller;
use Think\Conyroller;
class TestController exstends Controller
{


public function test()    // 使用ajax 方法验证
    {
       $y=D("YongHu");
       
       $arr=array(
       array('uid','require','用户名不能为空!'),
       );
       if($y->validate($arr)->create())
       {
                $y->add();
        $this->ajaxReturn("通过验证","eval");   
           
       }
       else
       {
           $this->ajaxReturn($y->getError(),"eval");
       }
           
    }
// tianjia.html 页面

 

public function add()
  {
    if(empty($_POST)) 判断POST有没有传值
      {

       $this->show();//没传值 显示页面
      }
      else
      {
         
         $y=D("Yonghu"); //注意大小写
         $r=$y->create(); //验证通过返回true
         if($r)
          {
             $y->add() //添加数据库
          }
         else
           {
            die($y->getError);//输出错误信息,并退出程序
           }
       
       } 
   }
}




//动态验证
 $y=D("YongHu");

$arr=array(  //造一个正则验证

  array("uid","require","用户名不能为空"),
 

);


 if($y->calidate($arr)->create())
//使用validate 把规则加进来 判断是否成功
{
$y->add();//使用Add 添加

}
else
{
die($y->getError());//输出错误信息,并退出程序
}



使用ajax 验证

 

posted on 2016-12-27 10:00  奔跑的葛根  阅读(190)  评论(0编辑  收藏  举报

导航