当前位置:Linux教程 - Php - 一个改写的表单验证类

一个改写的表单验证类

一个改写的表单验证类

代码:


<?php
/****************************************
*   FormChecker V0.3                   *
* By:Terpomo  Email:[email protected] *
* **************************************

* 主要函数:
* 1.增加一项验证项
* Add($id,$name,$type,$canNull=false,$max=100,$msg='',$regular='//');
*      $id 要检查的表单(数组)中键值
*   $name 该项的名称
*  $type 类别(text/int/email/url/postcode/phponecode/usr(自定义))
*  $canNull 可以为空,默认为不能
*  $max 最大长度
*  $msg 如果出错的提示信息,不填使用系统默认
*  $regular type为user时的自定义正则表达式
* 2.检查一个数组是否符合所有人验证项
* Check($Array)
* 3.检查结果
* checkPassed 是否通过检查
* 4.错误信息
* error
* 一个简单例子
*--------------------------------------------
* <?php
*  $FC = new FormChecker;
*  $FC->Add('email','电子邮件','email');
*  $FC->Check($_POST);
*  if($FC->checkPassed){
*   do some thing for the form values....
*  }else{
*   echo $FC->error;
*  }
*  ?>
* <form action=<? php_self ?> ><input type=text name=email><input type=submit></for>
*--------------------------------------------
* 注:写这个class是为了尝试如何方便的进行大量的表单的验证和处理,希望大家提出讨论的意见
*/


if(!defined("KOMETO_LIBRARY_FORMCHECKER"))
{
define("KOMETO_LIBRARY_FORMCHECKER",1);
class
FormChecker
{

/*  A checker Define as :
*    "type"=>"类型:text/int/email/url/postcode/phonecode/user,默认:text,自定义:user",
*  "id"=>"表单项名称",
*  "name' => "提示用名"
*  "maxLength"=> "最大长度,默认100,"
*  "canNull" => "true/false"
*  "errorMessage"=>"错误提示,可以为空,自动产生",
*  "regular"=>自定义正则表达式
**/
var $checkerList = array();

var
$typeRegularList = array(
      
'text'  => '//',
      
'int'   => '/^[0-9.]+$/',
      
'email' => '/^[a-zA-z0-9_]+?@[a-zA-z0-9_.]+?$/',
      
'url' => '',
      
'postcode' => '/^[0-9]{6}$/',
      
'phonecode' => '/^[0-9-())]+$/',
      );

var
$checkPassed = false;

var
$error = "";
var
$error_msg_isnull  = ":不能为空!";
var
$error_msg_regular = ":格式错误!";

function
AddError($s){
$chekcPassed = false;
$this->error = $this->error.$s.'<br>';
}

function
Add($id,$name,$type,$canNull=false,$max=100,$msg='',$regular='//'){
$A = array(
   
"id" => $id,
   
"name" => $name,
   
"type" => $type,
   
"canNull" => $canNull,
   
"max" => $max,
   
"msg" => $msg,
   
"regular" => $regular,
   );
  
$this->checkerList[] = $A;  
}
function
AddInputA($A){
$this->checkerList[] = $A;
}

function
Check($vars){
$this->checkPassed = true;
while (list (
$key, $val) = each ($vars)) {  
  
$checker = $this->GetChecker($key);
  
//Is Define Checker
  
if($checker){  
   
$currentVar = $checker['name'];
   
//Is Enter A value?
   
if($val == ''){     //Not Entered
    
if($checker['canNull']){
     continue;
    }else{
     
$error = $currentVar.$this->error_msg_isnull;
     
$this->AddError($error);
    }
   }else{            
// Enteered
    //Get Regular Express
    
if($checker['type'] == 'user'){
     
$regular = $checker['regular'];
    }else{     
     
$regular = $this->GetRegularOfType($checker['type']);
    }
    if(!
preg_match($regular,$val)){
     
$error = $currentVar.$this->error_msg_regular;
     
$this->AddError($error);
    }
   }
  }
  
}
}

function
GetChecker($id){
$cl = $this->checkerList;
$i = 0;
while(
$i < count($cl)){
  if(
$cl[$i]['id'] == $id){
   return
$cl[$i];
   exit;
  }
  
$i++;
}
return
false;  
}
function
GetRegularOfType($type){
if(isset(
$this->typeRegularList[$type])){
  return
$this->typeRegularList[$type];
}else{
  
$this->AddError("无效的类型:$type");
  return
"//";
}
}
}

}
?>