最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 表单验证代码
时间:2009-03-30 编辑:简简单单 来源:一聚教程网
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;
}
}
}
-
上一个: php 防注入与字符反过滤代码
-
下一个: php 编码转换程序代码
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24