form validation

WBOY
WBOYOriginal
2016-07-25 09:09:521027browse
  1. /**
  2. * Page function: Common form validation classes
  3. * Author: phper
  4. * Creation time: 2011-4-11
  5. */
  6. class validate{
  7. public static function isEmail($str){
  8. return is_string($str)&&preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str);
  9. }
  10. public static function isUrl($str){
  11. return is_string($str)&&preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str);
  12. }
  13. public static function isPhone($str,$type){
  14. $preg_array_pho=array(
  15. 'cn'=>'/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/',
  16. 'tw'=>''
  17. );
  18. if(in_array($type,array_keys($pre_array_pho))){
  19. return preg_match($pre_array_pho[$type],$str);
  20. }else{
  21. die($type.'-phone number is undefined');
  22. }
  23. }
  24. public static function isText($str,$type,$min_lenth=1,$max_lenth=''){
  25. $preg_array_text=array(
  26. 'ch'=>"/^([x81-xfe][x40-xfe]){".$min_lenth.",".$max_lenth."}$/",
  27. 'num'=>"/^[0-9]{".$min_lenth.",".$max_lenth."}$/i",
  28. );
  29. if(in_array($type,array_keys($preg_array_text))){
  30. return is_string($preg_array_text)&&preg_match($preg_array_text[$type],$str);
  31. }else{
  32. die($type.'-text is undefined');
  33. }
  34. }
  35. public static function isIp($ip){
  36. 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);
  37. }
  38. public static function isDate($var){
  39. return preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$var);
  40. }
  41. public static function isColor($var){
  42. return preg_match("/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/",$var);
  43. }
  44. public static function isUserName($var){
  45. return preg_match("/^[a-zA-Z0-9_.-]{3,16}$/",$var);
  46. }
  47. public static function isPic($var){
  48. return preg_match("/^[a-zA-Z0-9-.]+.(jpg|jpeg|gif|png)$/",$var);
  49. }
  50. }
  51. ?>
复制代码


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