Advertisement
Andronicuszeno

validation_functions.php

Apr 5th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2. // Vaildation Functions
  3.  
  4. // Presence validation
  5. function has_presence($value) {  // returns true if it has presence and false if empty.
  6.     return isset($value) && $value !== "";
  7. }
  8.  
  9.  
  10. // String length validation
  11. function has_maximum_length($value, $maxsize) { // returns true if object is less than maximum length
  12.     return strlen($value) <= $maxsize;
  13. }
  14.  
  15.  
  16. // Type validation
  17.  
  18. // Inclusion in a set validation
  19. function has_membership_in($value, $set) { // returns true if item belongs to the set.
  20.     return in_array($value, $set);
  21. }
  22.  
  23. function has_gender($value) { // returns true if gender is male or female.
  24.     $genderset = array("male", "female");
  25.     return in_array($value, $genderset);
  26. }
  27.    
  28. /* 
  29. // Format validation
  30. // preg_match($regex, $subject)
  31. if (preg_match( , )) {
  32.     // match found
  33. } else {
  34.     // match not found
  35. }
  36. */
  37.  
  38. // Display form errors
  39. function form_errors($errors=array()) {
  40.     $output = "";
  41.     if (!empty($errors)) {
  42.         $output .= "<div class=\"error\">";
  43.         $output .= "<ul>";
  44.         foreach ($errors as $key => $error) {
  45.             $output .= "<li>{$error}</li>";
  46.         }
  47.         $output .= "</ul>";
  48.         $output .= "</div>";
  49.     }
  50.     return $output;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement