您的位置:首页 > 其它

表单验证常用正则表达式

2011-04-24 11:26 387 查看
class validate{
public static function isEmail($str){
return is_string($str)&&preg_match('/^[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.)+[a-z]{2,4}$/',$str);
}
public static function isUrl($str){
return is_string($str)&&preg_match("/^http:////[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^<>/"/"])*$/",$str);
}
public static function isPhone($str,$type){
$preg_array_pho=array(
'cn'=>'/^((/(/d{3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}$/',
'tw'=>''
);
if(in_array($type,array_keys($pre_array_pho))){
return preg_match($pre_array_pho[$type],$str);
}else{
die($type.'-phone number is undefined');
}
}
public static function isText($str,$type,$min_lenth=1,$max_lenth=''){
$preg_array_text=array(
'ch'=>"/^([/x81-/xfe][/x40-/xfe]){".$min_lenth.",".$max_lenth."}$/",
'num'=>"/^[0-9]{".$min_lenth.",".$max_lenth."}$/i",
);
if(in_array($type,array_keys($preg_array_text))){
return is_string($preg_array_text)&&preg_match($preg_array_text[$type],$str);
}else{
die($type.'-text is undefined');
}
}
public static function isIp($ip){
return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" . "(/.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}
public static function isDate($var){
return preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$var);
}
public static function isColor($var){
return preg_match("/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/",$var);
}
public static function isUserName($var){
return preg_match("/^[a-zA-Z0-9_/./-]{3,16}$/",$var);
}
public static function isPic($var){
return preg_match("/^[a-zA-Z0-9/-/.]+/.(jpg|jpeg|gif|png)$/",$var);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: