表单验证常用正则表达式
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); } }
相关文章推荐
- 前端表单验证常用的15个JS正则表达式
- 常用的JavaScript验证正则表达式
- JS常用表单验证总结
- JS常用的一些表单验证函数
- 常用网站后台与前台表单的有效性复杂验证(net+jqury+ajax)不用验证控件方法
- 常用JavaScript(js)表单验证CheckList V1.0(beta)
- 表单验证常用正则(强烈推荐大家收藏下)
- js 常用正则表达式表单验证代码
- jQuery-JS常用表单验证&35个jQuery小技巧
- Ext:表单常用验证
- 常用的15个正则表达式验证表单
- 实例解析jQuery插件EasyUI最常用的表单验证规则
- 常用表单数据的验证方法
- html中常用JavaScript验证表单文本域方法,如日期、字符串、数字等
- 注册表单常用验证方式
- 最常用的15个JS正则表达式(前端表单验证)
- JavaScript表单常用验证集合
- JS表单验证的代码(常用)
- 表单验证常用正则(强烈推荐大家收藏下)
- 【正则表达式图片总结】+常用的表单验证代码