zoukankan      html  css  js  c++  java
  • thinkphp 验证

    //YongHuModel.class.php
    namespace HomeModel;
    
    use ThinkModel;
    
    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}((0d)|(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 HomeController;
    use ThinkConyroller;
    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 验证
  • 相关阅读:
    清北刷题班day3 morning
    [NOI1997] 积木游戏(dp)
    [NOI1999] 棋盘分割(推式子+dp)
    2017北京国庆刷题Day7 afternoon
    湖南集训day8
    湖南集训day7
    湖南集训day6
    湖南集训day5
    湖南集训day4
    湖南集训day3
  • 原文地址:https://www.cnblogs.com/benpaodegegen/p/6224817.html
Copyright © 2011-2022 走看看