login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Reguliere expressies > Valideer input met regular expressions

Valideer input met regular expressions

Auteur: Tri - 08 januari 2005 - 15:00 - Gekeurd door: Dennisvb - Hits: 5265 - Aantal punten: 3.75 (4 stemmen)



Een voorbeeld wordt genoemd in het script, hoe je het moet gebruiken.

Tri

Code:
  1. <?PHP
  2. /**
  3. * Validate input by format.
  4. *
  5. * Validate your input with this simple and small class.
  6. * I made use of Perl-compatible regular expressions,
  7. * because they are often faster than their ereg* counterparts.
  8. * You can use this class 'out-of-the-box'.
  9. *
  10. * Example usage:
  11. * <?PHP
  12. * require_once('validatefields.class.php');
  13. * // No need to create an instance of the class,
  14. * // because we use static functions (PHP5's new feature).
  15. * $varname = 'email@address.com';
  16. * if(validateFields::validateEmail($varname))
  17. * { print 'Valid e-mailaddress.'; }
  18. * else { print 'Not a valid e-mailaddress.'; }
  19. * ?>
  20. *
  21. * @author Tri Pham <tri[at]tripham.nl>
  22. * @link http://www.tripham.nl/
  23. * @since 30 december 2004
  24. * @version 0.1
  25. * @todo Add more functions such as date and time functions.
  26. * More options such as offset parameters.
  27. */
  28.  
  29.  
  30. /**
  31. * This is the actual code class.
  32. *
  33. * @since 29 december 2004
  34. */
  35.  
  36. class validateFields {
  37.  
  38.  
  39. /**
  40. * Validate input. Only characters allowed.
  41. * @param string $pString
  42. * @return mixed
  43. */
  44. public static function onlyCharacters($pString) {
  45.  
  46. return(preg_match("/^[aA-zZ]+$/", $pString));
  47.  
  48. }
  49.  
  50. /**
  51. * Validate input. Only digits allowed.
  52. * @param int $pDigits
  53. * @return mixed
  54. */
  55. public static function onlyDigits($pDigits) {
  56.  
  57. return(preg_match("/^[0-9]+$/", $pDigits));
  58.  
  59. }
  60.  
  61. /**
  62. * Validate e-mailaddress.
  63. * @param string $pEmail
  64. * @return mixed
  65. */
  66. public static function validateEmail($pEmail) {
  67.  
  68. // The TLD (Top Level Domain) can be 2 or 4 characters long.
  69. // Example: .nl and .info
  70.  
  71. return(preg_match("/[a-z0-9.-]+@[a-z0-9.-]+\.[a-z]{2,4}/i", $pEmail));
  72.  
  73. }
  74.  
  75. /**
  76. * Validate Dutch zipcodes.
  77. * @param mixed $pZipcode
  78. * @return mixed
  79. */
  80. public static function validateDutchZipcode($pZipcode) {
  81.  
  82. // Validate Dutch zipcodes.
  83. // Valid zipcodes (without the single quotes of course):
  84. // '1337TP' and '1337 TP'
  85.  
  86. return(preg_match("/^[0-9]{4}\s?[A-Z]{2}$/", strtoupper($pZipcode)));
  87.  
  88. }
  89.  
  90. /**
  91. * Validate Dutch thelephone numbers.
  92. * @param mixed $pPhonenumber
  93. * @return mixed
  94. */
  95. public static function validateDutchPhoneNumber($pPhonenumber) {
  96.  
  97. // Some Dutch phonenumbers start off with 3 digits,
  98. // while other start off with 4 digits.
  99. // Valid Dutch phonenumbers: 1337-123456 and 101-1234567
  100.  
  101. return(preg_match("/^(\d{3}-?\d{7}|\d{4}-?\d{6})$/", $pPhonenumber));
  102.  
  103. }
  104.  
  105. /**
  106. * Validate IP version 4 addresses.
  107. * @param mixed $IPaddress
  108. * @return mixed
  109. */
  110. public static function validateIPv4($pIPaddress) {
  111.  
  112. // I got this regular expression from the preg_match() comments.
  113.  
  114. /**
  115.   * @var string $sPattern Our pattern.
  116.   */
  117. $sPattern = "([0-9]|^1?\d\d$|2[0-4]\d|25[0-5])";
  118.  
  119. return(preg_match("/$sPattern\.$sPattern\.$sPattern\.$sPattern/", $pIPaddress));
  120. }
  121.  
  122. /**
  123. * Validate URL.
  124. * @param string $pURL
  125. * @return mixed
  126. */
  127. public static function validateURL($pURL) {
  128.  
  129. // Een URL matchen is een hell!!
  130.  
  131. // Valid URL's:
  132. // http://www.tripham.nl/
  133. // http://tripham.nl/
  134. // http://security.tripham.nl/
  135. // https://www.tripham.nl:80/
  136. // http://1337.ditbestaatniet.tripham.nl
  137. // http://blaat:aap@tripham.nl:80
  138. // The last slash is optional.
  139.  
  140. // Regular expressions for traditional URL's.
  141. //return(preg_match("/^https?:\/\/(www|[a-z])?\.?[a-z0-9\-]+\.([a-z]{2,4}\/?)$/i", $pURL));
  142.  
  143. return(preg_match("/^https?:\/\/([a-z0-9\-@]+\.?):?\.([a-z]{2,4}|[a-z]{2,4}:[0-9]{1,6})\/?$/i", $pURL));
  144. }
  145. }
  146.  
  147. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (5)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.026s