Home >php教程 >php手册 >php 表单验证代码

php 表单验证代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:22:291078browse

提供一款检查php表单检证与检查效果代码。

function validate($value,$onoff=0,$type=3,$len=100,$msg='填写错误') {


if(trim($value) != ""){    //如果有内容  

if(!is_int($len))
exit("");

preg_match_all("/[xa0-xff]?./",$value,$arr); //中英文混排算字符串长度
$qswhLen_v=count($arr[0]);   
if($qswhLen_v> $len)
        {
            exit("");
 
        }   
      
if ($type==0){  //类型为0的话 检查数据是否为数字
 if(!eregi('^[0-9]+$', $value)){
  exit("");
        }      
}
else if ($type==1){   //类型为1的话 检查数据是否为字母
 if(!eregi('^[a-zA-Z]+$',$value)){
        exit("");
        }
}
else if ($type==2){ //类型为2的话 检查数据是否为中文
 if(!eregi("^[".chr(0xa1)."-".chr(0xff)."]+$",$value)){
        exit("");
        }
}
else if ($type==3){ //类型为3的话 检查数据是否为字母和数字
 if(!eregi('^[0-9a-zA-Z]+$',$value)){
        exit("");
        }
}
else if ($type==4){ //类型为4的话 email检查
 if(!eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z_-]+.)+[a-zA-Z]$", $value)){
        exit("");
        }
}
else if ($type==5){ //类型为5的话 日期格式检查
 if(!eregi('^[1-2]{1}[0-9]{3}-[0-9]{2}-[0-9]{2}',$value)){
        exit("");
        }
}
else if ($type==6){ //类型为6的话 身份证号码格式检查
 if(!preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$value)){
        exit("");
        }
}
else if ($type==7){ //类型为7的话 电话号码格式检查
 if(!preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$value)){
        exit("");
        }
}
else if ($type==8){ //类型为8的话 邮编格式检查
 if(!preg_match("/^[1-9]d{5}$/",$value)){
        exit("");
        }
}
else if ($type==9){ //类型为9的话 url地址格式检查
 if(!preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^""])*$/",$value)){
        exit("");
        }
}
else if ($type==10){ //类型为10的话 无限制

}else{
echo "";
exit;
}  
       
        }else{
  if($onoff!=0){
echo "";
        exit;
        }
}

}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn