Simple Validation Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /*************************************************************************
  4. *** Example of Usage: ****************************************************
  5. ***** Validate::string('717 123 4567',Validate::SPACE.Validate::PHONE); **
  6. ***** returns true *******************************************************
  7. *************************************************************************/
  8.  
  9. class Validate
  10. {
  11.  
  12. const name = 'Simple Validation Class';
  13. const version = '0.2a';
  14.  
  15. const NUM = '0-9';
  16. const SPACE = '\s';
  17. const ALPHA_LOWER = 'a-z';
  18. const ALPHA_UPPER = 'A-Z';
  19. const ALPHA = 'a-zA-Z';
  20. const EALPHA_LOWER = 'a-z�¡�©�­�³�º�½� �¨�¬�²�¹�¤�«�¯�¶�¼�¿�¢�ª�®�´�»�£�±�µ�¡�¥�¦�§���°�¸�¾��';
  21. const EALPHA_UPPER = 'A-Z���������������������������������¸����������������� ��������������';
  22. const EALPHA = 'a-z�¡�©�­�³�º�½� �¨�¬�²�¹�¤�«�¯�¶�¼�¿�¢�ª�®�´�»�£�±�µ�¡�¥�¦�§���°�¸�¾��A-Z���������������������������������¸����������������� ��������������';
  23. const PUNCTUATION = '\s\.\-_,;\/\:&"\'\?\!\(\)';
  24. const ALL = '0-9\s\.\-_,;\/\:&"\'\?\!\(\)a-z�¡�©�­�³�º�½� �¨�¬�²�¹�¤�«�¯�¶�¼�¿�¢�ª�®�´�»�£�±�µ�¡�¥�¦�§���°�¸�¾��A-Z���������������������������������¸����������������� ��������������';
  25. const PHONE = '0-9\s+\-.\(\)#\*\/';
  26.  
  27. static private function version_check() {
  28. return version_compare(PHP_VERSION, '5.2.0', '>=');
  29. }//end function
  30.  
  31. public function url ($url, $length = 12, $http=TRUE) {
  32. if (strlen($url) < $length) return false;
  33. if (self::version_check()) {
  34. $flag = null;
  35. if ($http) $flag = FILTER_FLAG_PATH_REQUIRED;
  36. return (bool) filter_var($integer, FILTER_VALIDATE_URL, $flag);
  37. }//end if
  38. return !preg_match('/ /', $url)
  39. && preg_match('|^'.($http?'http(s)?://':'').'[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
  40. }//end function
  41.  
  42. public function string ($string, $charset=self::ALL, $length=1) {
  43. return strlen($string) >= $length
  44. && preg_match("|^[$charset]*\$|s", $string);
  45. }//end function
  46.  
  47. public function integer ($integer, $min=null, $max=null) {
  48. if (self::version_check()) {
  49. $options = array('default'=>false);
  50. if (isset($min)) $options['min_range'] = $min;
  51. if (isset($max)) $options['max_range'] = $max;
  52. return filter_var($integer, FILTER_VALIDATE_INT, $options);
  53. }//end if
  54. return is_int($integer) && (!isset($min) || $integer>=$min) && (!isset($max) || $integer<=$max);
  55. }//end function
  56.  
  57. public function email ($email) {
  58. if (self::version_check()) return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
  59. $broken = explode('@',$email);
  60. return empty($broken[2])
  61. && self::url($broken[1],5,FALSE)
  62. && self::string($broken[0],self::ALPHA.self::NUM.'\.\-_');
  63. }//end function
  64.  
  65. }
  66.  
  67. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.