fork download
  1. <?php
  2. class validate {
  3.  
  4. // ---------------------------------------------------------------------------
  5. // paramaters
  6. // ---------------------------------------------------------------------------
  7.  
  8. /**
  9.   * Array to hold the errors
  10.   *
  11.   * @access public
  12.   * @var array
  13.   */
  14. public $errors = array();
  15.  
  16. // ---------------------------------------------------------------------------
  17. // validation methods
  18. // ---------------------------------------------------------------------------
  19.  
  20. /**
  21.   * Validates a string
  22.   *
  23.   * @access public
  24.   * @param $postVal - the value of the $_POST request
  25.   * @param $postName - the name of the form element being validated
  26.   * @param $min - minimum string length
  27.   * @param $max - maximum string length
  28.   * @return void
  29.   */
  30. public function validateStr($postVal, $postName, $min = 5, $max = 500) {
  31. if(strlen($postVal) < intval($min)) {
  32. $this->setError($postName, ucfirst($postName)." must be at least {$min} characters long.");
  33. } else if(strlen($postVal) > intval($max)) {
  34. $this->setError($postName, ucfirst($postName)." must be less than {$max} characters long.");
  35. }
  36. }// end validateStr
  37.  
  38. /**
  39.   * Validates an email address
  40.   *
  41.   * @access public
  42.   * @param $emailVal - the value of the $_POST request
  43.   * @param $emailName - the name of the email form element being validated
  44.   * @return void
  45.   */
  46. public function validateEmail($emailVal, $emailName) {
  47. if(strlen($emailVal) <= 0) {
  48. $this->setError($emailName, "Please enter an Email Address");
  49. } else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $emailVal)) {
  50. $this->setError($emailName, "Please enter a Valid Email Address");
  51. }
  52. }// end validateEmail
  53.  
  54. // ---------------------------------------------------------------------------
  55. // error handling methods
  56. // ---------------------------------------------------------------------------
  57.  
  58. /**
  59.   * sets an error message for a form element
  60.   *
  61.   * @access private
  62.   * @param string $element - name of the form element
  63.   * @param string $message - error message to be displayed
  64.   * @return void
  65.   */
  66. private function setError($element, $message) {
  67. $this->errors[$element] = $message;
  68. }// end logError
  69.  
  70. /**
  71.   * returns the error of a single form element
  72.   *
  73.   * @access public
  74.   * @param string $elementName - name of the form element
  75.   * @return string
  76.   */
  77. public function getError($elementName) {
  78. if($this->errors[$elementName]) {
  79. return $this->errors[$elementName];
  80. } else {
  81. return false;
  82. }
  83. }// end getError
  84.  
  85. /**
  86.   * displays the errors as an html un-ordered list
  87.   *
  88.   * @access public
  89.   * @return string: A html list of the forms errors
  90.   */
  91. public function displayErrors() {
  92. $errorsList = "<ul class=\"errors\">\n";
  93. foreach($this->errors as $value) {
  94. $errorsList .= "<li>". $value . "</li>\n";
  95. }
  96. $errorsList .= "</ul>\n";
  97. return $errorsList;
  98. }// end displayErrors
  99.  
  100. /**
  101.   * returns whether the form has errors
  102.   *
  103.   * @access public
  104.   * @return boolean
  105.   */
  106. public function hasErrors() {
  107. if(count($this->errors) > 0) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }// end hasErrors
  113.  
  114. /**
  115.   * returns a string stating how many errors there were
  116.   *
  117.   * @access public
  118.   * @return void
  119.   */
  120. public function errorNumMessage() {
  121. if(count($this->errors) > 1) {
  122. $message = "There were " . count($this->errors) . " errors sending your message!\n";
  123. } else {
  124. $message = "There was an error sending your message!\n";
  125. }
  126. return $message;
  127. }// end hasErrors
  128.  
  129. }// end class
  130. $_POST['name'] = '';
  131. $_POST['email'] = '';
  132. $_POST['message'] = '';
  133. $_POST['submit'] = true;
  134. define("EMAIL", "ITSPRIVATE@outlook.com");
  135.  
  136. if(isset($_POST['submit'])) {
  137.  
  138. // include('validate.class.php');
  139.  
  140. //assign post data to variables
  141. $name = trim($_POST['name']);
  142. $email = trim($_POST['email']);
  143. $message = trim($_POST['message']);
  144.  
  145. //start validating our form
  146. $v = new validate();
  147. $v->validateStr($name, "name", 3, 75);
  148. $v->validateEmail($email, "email");
  149. $v->validateStr($message, "message", 5, 1000);
  150.  
  151. if(!$v->hasErrors()) {
  152. echo 'has no errors';
  153. $header = "From: $email\n" . "Reply-To: $email\n";
  154. $subject = "Contact Form Subject";
  155. $email_to = EMAIL;
  156.  
  157. $emailMessage = "Name: " . $name . "\n";
  158. $emailMessage .= "Email: " . $email . "\n\n";
  159. $emailMessage .= $message;
  160.  
  161. //use php's mail function to send the email
  162. // @mail($email_to, $subject ,$emailMessage ,$header );
  163.  
  164. //grab the current url, append ?sent=yes to it and then redirect to that url
  165. // $url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  166. // header('Location: '.$url."?sent=yes");
  167.  
  168. } else {
  169. echo 'has errors';
  170. //set the number of errors message
  171. $message_text = $v->errorNumMessage();
  172.  
  173. //store the errors list in a variable
  174. $errors = $v->displayErrors();
  175.  
  176. //get the individual error messages
  177. $nameErr = $v->getError("name");
  178. $emailErr = $v->getError("email");
  179. $messageErr = $v->getError("message");
  180. }//end error check
  181. }// end isset
  182. echo $nameErr . "\n";
  183. echo $emailErr . "\n";
  184. echo $messageErr . "\n";
  185. echo $errors . "\n";
  186. echo $message_text;
  187.  
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
has errorsName must be at least 3 characters long.
Please enter an Email Address
Message must be at least 5 characters long.
<ul class="errors">
<li>Name must be at least 3 characters long.</li>
<li>Please enter an Email Address</li>
<li>Message must be at least 5 characters long.</li>
</ul>

There were 3 errors sending your message!