php 验证类(张艳冲的博客)

//  这是一个方法    http://localhost/feelappdemo/index.php?c=site&a=adduser 路径下 
    public function actionAddUser()

    {
        $model = new publicModel();
           
        $error=array();  //错误信息  
        if(isset($_POST['register'])){  //[user] => [passwd] => [repassword] => [address] => [postcode] => [url] =>                
        //    print_r($_POST['register']);
          $post=$_POST['register'];
         $kong =array(
              'user'=>array($_POST['register']['user'],'用户名不能为空'),
              'passwd'=> array($_POST['register']['passwd'],'密码不能为空'),
              'repassword'=>array($_POST['register']['repassword'],'确定密码不能为空'),  
               'jine'=> array($_POST['register']['passwd'],'金额不能为空'),
              'url'=>array($_POST['register']['url'],'网址不能为空'),    
              'qq'=>array($_POST['register']['qq'],'QQ不能为空'),      
                );  
        $model->kongValidator($kong);
        $model->lengthValidator('user',$_POST['register']['user'],3,0,'最少三个字符');
        $model->compareValidator('tocompare',$_POST['register']['passwd'],$_POST['register']['repassword'],'俩次密码不一样');
        $model->moneyValidator('jine',$_POST['register']['jine'],'请这个正确填写金额');
        $model->urlValidator('url',$_POST['register']['url'],'请这个正确填写网址');    
        $model->qqValidator('qq',$_POST['register']['qq'],'请这个正确填写QQ');    
         // print_r($error);    
          if(empty($model->errors)){
                echo "成功!";
            }
        }
    $this->render('addUser',array('error'=>$error,'model'=>$model,'post'=>$post));
    }



// php验证类

<?php

class publicModel extends ParentModel
{
  public $post=array();   
  public $errors=array();   //所有的错误   
//构造方法自动给错误的定义成model中$erroe变量   
    public  function __set($name,$value){
        if(is_array($this->errors)){   //把错误添加到错误数组中
            $this->errors[$name]=$value;
                     
    }
//得到每个错误
 public  function  getError($key){
         return isset($this->errors[$key]) ? $this->errors[$key] : '';    
 }
//判断某个属性是否有错误信息
    public function hasError($key)
    {
        return isset($this->errors[$key]);
   }

//向类属性中错误信息
public function addError($key,$value)
{
        $this->errors[$key] = $value;
  

// 多个判断空的方法
  public function  kongValidator($array){
              if(is_array($array)){
                   foreach ($array  as $k=>$val){
                        if(trim($val[0])==''){
                            $this->$k=$val['1'];
                                            
                        
              }
  }
         
    public function requiredValidator($key,$attribute,$message)
    {
        if(empty($attribute)){
          $this->errors[$key]=$message;
        }
    }
 
   
   
    public function lengthValidator($key,$attribute,$min=0,$max=0,$message)
    {
           if(!$this->hasError($key)){   
                        if(!empty($attribute)){
                            $strlen =$this->strlength(trim($attribute));
                            if(($min!=0 && $max==0)){
                                if($strlen < $min){
                                      $this->errors[$key]=$message;
                                }
                            }else if(($min==0 && $max!=0)){
                                if($strlen > $max){
                                      $this->errors[$key]=$message;
                                }
                            }else if($min!=0 && $max!=0){
                                if($strlen <$min || $strlen > $max){
                                      $this->errors[$key]=$message;
                                }
                            }
                        }
            
    }

   
    public function emailValidator($key,$attribute,$message)
    {
       if(!$this->hasErrors($key)){   
                $pattern = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
                if(!$this->regex($attribute,$pattern)){
                     $this->errors[$key]=$message;
                }
       }        
    }
   
    public function compareValidator($key,$oringal,$tocompare,$message)
         
           if(($oringal!='') && ($tocompare!='')){
                   if ($oringal!=$tocompare){
                        $this->errors[$key]=$message;
                   }
           }
    }
   
    public function urlValidator($key,$attribute,$message)
    {
        $pattern = '/^(http:\/\/|https:\/\/)?\w{1,}(\.[a-zA-Z0-9\-:;]{1,})+(\/[\w-\.\/:;=\?&%]*)*$/';
        if(!$this->hasError($key)){
            if(!$this->regex($attribute,$pattern)){
                $this->errors[$key]=$message;
            }
        }
     
   
   
    public function mobileValidator($key,$attribute,$message)
    {
        if(!$this->hasError($key)){
            if($attribute != ''){
                $pattern = '/^1\d{10}$/';   
                    if(!$this->regex($attribute,$pattern))
                    $this->errors[$key]=$message;
                         
        }
     
   
   
   
    public function qqValidator($key,$attribute,$message)
    {
        if(!$this->hasError($key)){
            if($attribute != ''){
                $pattern = '/^\d{4,11}$/';
                    if(!$this->regex($attribute,$pattern))
                    $this->errors[$key]=$message;
            }
        }
       
     

   
    public function numValidator($key,$attribute,$message)
    {
        if(!$this->hasError($key)){
            if($attribute != ''){
                $pattern = '/^\d+$/';
                    if(!$this->regex($attribute,$pattern))
                    $this->errors[$key]=$message;
            }
        }
       
         
   
    public function moneyValidator($key,$attribute,$message)
    {
        if(!$this->hasError($key)){
            if($attribute != ''){
                $pattern= '/^\d{1,30}\.*\d{1,2}$/';
                    if(!$this->regex($attribute,$pattern))
                    $this->errors[$key]=$message;
            }
        }
       
     
   
   
   
    private function regex($attribute,$pattern)
    {
        if(!empty($attribute)){
            return preg_match($pattern,$attribute);
        }
        return true;
    }

//判断字符串长度函数
function strlength($str)
{
        if(empty($str)){
            return 0;
        }
        if(function_exists('mb_strlen')){
            return mb_strlen($str,'utf-8');
    }
    else {
            preg_match_all("/./u", $str, $ar);
            return count($ar[0]);
        }
    }
   

}


posted @ 2012-03-30 15:42  张三_zhangsan  阅读(139)  评论(0编辑  收藏  举报