title: PHP preg_match Form Validation
body: <p>I am having difficulties with my php code which validates the form on the page itself before sending out to my email.</p>
  
  <p>This is my PHP which is above the doctype html</p>
  
  <pre><code>&lt;?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subjectTitle = $_POST['subjectTitle'];
  $comment = $_POST['comment'];   
  $errormsg = "Invalid Entry";
  $validmsg = "Thank you for your message";
  
  if($_POST['submit']) {  
      if(empty($_POST['name'])) {     
          $nameError = true;  
      } 
      if(empty($_POST['email'])) {        
          $emailError = true; 
      }       
      if(empty($_POST['subjectTitle'])) {     
          $subjectError = true;   
      }       
      if(empty($_POST['comment'])) {
          $commentError = true;   
      } 
      else if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subjectTitle']) || empty($_POST['comment'])) {    
          $error = true;      
      }               
      else {          
          $to = "EMAIL_ADD_GOES_HERE";        
          $name = trim($_POST['name']);
          $email = trim($_POST['email']);
          $subjectTitle = trim($_POST['subjectTitle']);
          $comment = trim($_POST['comment']);         
          $subject = "Contact Form Enquiry";          
          $messages = "\r\n Name: $name \r\n Email: $email \r\n Subject: $subjectTitle \r\n Comments: $comment";
          $headers = "From:" . $name;
          $mailsent = mail($to, $subject, $messages, $headers);       
          if($mailsent) {         
              $sent = true;   
              $name = "";        
              $email = "";
              $subjectTitle = "";
              $comment = "";                          
          }           
      }   
  }
  ?&gt;
  </code></pre>
  
  <p>and for my HTML , to make viewing easier, I'll just put in the form part.</p>
  
  <pre><code>&lt;html&gt;
  &lt;form action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;" method="post"&gt;
       Name: &lt;input type="text" name="name" id="name" value="&lt;?php echo $name; ?&gt;"&gt; 
       &lt;?php if($nameError == true) {echo $errormsg;} ?&gt;
  
       Email: &lt;input type="text" name="email" id="email" value="&lt;?php echo $email; ?&gt;"&gt; 
       &lt;?php if($emailError == true) {echo $errormsg;} ?&gt;
  
       Subject: &lt;input type="text" name="subjectTitle" id="subjectTitle" value="&lt;?php echo $subjectTitle; ?&gt;"&gt; 
       &lt;?php if($subjectError == true) {echo $errormsg;} ?&gt;
  
       Comments: &lt;textarea name="comment" id="comment"&gt;&lt;?php echo $comment; ?&gt;&lt;/textarea&gt;
       &lt;?php if($commentError == true) {echo $errormsg;} ?&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>There is no other php code in my html other than stated above.</p>
  
  <p>This validation only works if any field is left empty. It will prompt the error msg within the page itself and would NOT send in the email, which is how it should work..</p>
  
  <p>However, I am facing a weird problem which I can't seem to find out the reason/theory/logic behind it. If u look at the php code, if I didn't have that else if statement</p>
  
  <pre><code>else if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subjectTitle']) || empty($_POST['comment'])) {        
      $error = true; 
  }
  </code></pre>
  
  <p>the validation fails and will still send in an email when the user keys in the textarea. For example, if the user doesn't fill in the Name, Email and Subject but only the Comments, it will still get sent. However, once i put that else if statement, it fixes the problem. </p>
  
  <p>But the main problem is, preg_match. I want the name fields to allow only space and letters and I want email field to only allow emails. So when I tried to put this code in,</p>
  
  <pre><code>if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameError = true;
  }
  </code></pre>
  
  <p>or by combing it with the name empty field check</p>
  
  <pre><code>if (empty($_POST['name']) || (!preg_match("/^[a-zA-Z ]*$/",$name))) {
      $nameError = true;      
  } 
  </code></pre>
  
  <p>it doesn't validate correctly. What it does is that, when a user enters a number into the name field and also fills up everything else, it will display error message and also the thank you message and send it to the email, which is not how it should work.</p>
  
  <p>So can I have guidance on how to correct the problem? Is my coding done wrongly and also I would like to know why that else if statement which I have in php makes no sense yet I need that to get the proper validation.</p>
  
  <p>Thank you so much for reading, been stuck on this for the past 6 days and I really dont know how to fix this. Please help me. </p>
  
  <p>Cheers</p>