分类:
2009-12-30 18:18:29
class Class_Check{
var $fields; //所有可能的输出域
function Class_Check($fields){
$this->fields=$fields;
}
var $formFields; //本表单的输出域
function reset(){
$this->formFields=array();
}
function formSubmit(){
$ret="onsubmit='";
foreach($this->formFields as $name=>$key){
$reg=$this->fields[$key][0];
$name="check_".$key."_".$name;
$ret.="ele=document.getElementById(\"$name\");";
$ret.="if(!new RegExp(/^$reg$/).test(ele.value)){";
$ret.=" ele.onblur();";
$ret.=" ele.focus();";
$ret.=" return false;";
$ret.="}";
}
$ret.="' ";
return $ret;
}
function output($key,$name,$default='',$class=''){
$this->formFields[$name]=$key;
$reg=$this->fields[$key][0];
$msg=$this->fields[$key][1];
$name="check_".$key."_".$name;
$msgName=$name."_msg";
$output=";
if($default)$output.=" value='$delete' ";
if($class)$output.=" class='$class' ";
$output.=" onblur='document.getElementById(\"$msgName\").innerHTML=(new RegExp(/^$reg$/).test(this.value) == false)?\"$msg\":\"\";' ";
$output.=" />";
$output.=" ";
return $output;
}
function valid($trans){
if(!$_POST) return false;
$ret=array();
foreach($_POST as $postKey=>$value){
if(substr($postKey,0,6)!='check_')continue;
list($null,$key,$name)=explode('_',$postKey);
$reg=$this->fields[$key][0];
$msg=$this->fields[$key][1];
if(!preg_match("/^".$reg."$/",$value)){
if(isset($trans[$name]))
$ret[$trans[$name]]=$msg;
else
$ret[$name]=$msg;
}
}
if(count($ret)==0)return true;
return $ret;
}
}
?>