Advertisement
Guest User

Untitled

a guest
Mar 27th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?
  2.  
  3. // Start the main function
  4. StartValidate();
  5.  
  6. function StartValidate() {
  7.    
  8.     // Assign some var's for the requests
  9.     $required = $_GET["required"];
  10.     $type = $_GET["type"];
  11.     $value = $_GET["value"];
  12.  
  13.     // This is the function to check if a field is even required or not
  14.     // So it's useful if you only want to check if it isn't empty
  15.     validateRequired($required, $value, $type);
  16.  
  17.     switch ($type) {
  18.         case 'number':
  19.             validateNumber($value);
  20.             break;
  21.         case 'alphanum':
  22.             validateAlphanum($value);
  23.             break;
  24.         case 'alpha':
  25.             validateAlpha($value);
  26.             break;
  27.         case 'date':
  28.             validateDate($value);
  29.             break;
  30.         case 'email':
  31.             validateEmail($value);
  32.             break;
  33.         case 'url':
  34.             validateUrl($value);
  35.             break;
  36.     }
  37. }
  38.  
  39. // The function to check if a field is required or not
  40. function validateRequired($required, $value, $type) {
  41.     if($required == "required") {
  42.  
  43.         // Check if we got an empty value
  44.         if($value == "") {
  45.             echo "false";
  46.             exit();
  47.         }
  48.     } else {
  49.         if($value == "") {
  50.             echo "none";
  51.             exit();
  52.         }
  53.     }
  54. }
  55.  
  56. // I use regular expressions in order to check a field's input, you can
  57. // get most of them at the Regex Library at http://www.regexlib.com
  58. // There you can check your own regular expressions, too
  59.  
  60. // Validation of an Email Address
  61. function validateEmail($value) {
  62.     if(ereg("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $value, $regs)) {
  63.         echo "true";
  64.     } else {
  65.         echo "false";
  66.     }
  67. }
  68.  
  69. // Validation of a date
  70. function validateDate($value) {
  71.     if(ereg("^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$", $value, $regs)) {
  72.         echo "true";
  73.     } else {
  74.         echo "false";
  75.     }
  76. }
  77.  
  78. // Validation of an URL
  79. function validateUrl($value) {
  80.     if(ereg("^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$", $value, $regs)) {
  81.         echo "true";
  82.     } else {
  83.         echo "false";
  84.     }
  85. }
  86.  
  87. // Validation of characters
  88. function validateAlpha($value) {
  89.     if(ereg("^[a-zA-Z]+$", $value, $regs)) {
  90.         echo "true";
  91.     } else {
  92.         echo "false";
  93.     }
  94. }
  95.  
  96. // Validation of characters and numbers
  97. function validateAlphanum($value) {
  98.     if(ereg("^[a-zA-Z0-9]+$", $value, $regs)) {
  99.         echo "true";
  100.     } else {
  101.         echo "false";
  102.     }
  103. }
  104.  
  105. // Validation of numbers
  106. function validateNumber($value) {
  107.     if(ereg("^[0-9]+$", $value, $regs)) {
  108.         echo "true";
  109.     } else {
  110.         echo "false";
  111.     }
  112. }
  113.  
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement