PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > Various validation class

Various validation class

Created by Jose Chafardet, last update on 30/10/2008 11:37

This is yet again a small class i wrote (still working on it) that validates some input data (not perfect tho, i know, but im still working on it).

  1. //==================
  2. // Define class name
  3. class Validation {
  4. //===========
  5. // Properties
  6. //==================
  7. // Public properties
  8. public $str;
  9. public $length;
  10. //========
  11. // Methods
  12. //============================
  13. // Public method alpha_numeric
  14. public function alpha_numeric($str, $length) {
  15. if (empty ( $str )) {
  16. return false;
  17. }
  18. if (strlen ( $str ) < $length) {
  19. return false;
  20. }
  21. return (! preg_match ( "/^([-a-z0-9])+$/i", $str )) ? FALSE : TRUE;
  22. } // end public method alpha_numeric
  23. //======================================================
  24. // Public method valid_email
  25. // validates to accept only valid email addresses,
  26. // requiring an @ a . and at the very least 2 TLD leters
  27. public function valid_email($str) {
  28. if (empty ( $str )) {
  29. return false;
  30. }
  31. return (! preg_match (
    "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"
    , $str )) ? FALSE : TRUE;
  32. } // end public method valid_email
  33. //========================================
  34. // Public method numeric
  35. // validates to accept only numeric values
  36. public function numeric($str, $length) {
  37. if (empty ( $str )) {
  38. return false;
  39. }
  40. if (strlen ( $str ) < $length) {
  41. return false;
  42. }
  43. return (! preg_match ( "/^([0-9])+$/i", $str )) ? FALSE : TRUE;
  44. } // end public method numeric
  45. //========================================
  46. // public method latin_chars
  47. public function latin_chars($str) {
  48. $html_entities = array (
  49. "&" => "&",
  50. "á" => "",
  51. "Â" => "",
  52. "â" => "",
  53. "Æ" => "",
  54. "æ" => "",
  55. "À" => "",
  56. "à" => "",
  57. "Å" => "",
  58. "å" => "",
  59. "Ã" => "",
  60. "ã" => "",
  61. "Ä" => "",
  62. "ä" => "",
  63. "Ç" => "",
  64. "ç" => "",
  65. "É" => "",
  66. "é" => "",
  67. "Ê" => "",
  68. "ê" => "",
  69. "È" => "",
  70. "í" => "",
  71. "Í" => "",
  72. "ó" => "",
  73. "Ó" => "",
  74. "ö" => "",
  75. "ú" => "",
  76. "Ú" => "",
  77. "û" => "",
  78. "Ù" => "",
  79. "ù" => "",
  80. "Ü" => "",
  81. "ü" => "",
  82. "Ý" => "",
  83. "ý" => "",
  84. "ÿ" => "",
  85. "Ÿ" => "&Yuml;",
  86. "ñ" => "",
  87. "Ñ" => "",
  88. );
  89. foreach ($html_entities as $key => $value) {
  90. $str = str_replace($key, $value, $str);
  91. }// end foreach
  92. return $str;
  93. } // end public method latin_chars
  94. //=============================
  95. // Public method sanitize_mysql
  96. // sanitize data input
  97. public function sanitize($str) {
  98. $str = mysql_real_escape_string ( $str );
  99. return $str;
  100. } // end public method sanitize_mysql
  101. } // end class

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit