title: PHP form email validation
body: <p>My mail form is still sending emails even if the email address is not valid.
  For example, if I fill in the email as "bob", and hit submit, my javascript validator gives a warning message, but the email still goes through. It ends up in my spam box as bob@mydomain.com</p>
  
  <p>How can I validate the email address, and prevent submit if it does not validate?</p>
  
  <p>I am new to php.</p>
  
  <p>HTML:</p>
  
  <pre><code> &lt;div id="emailform"&gt;
                  &lt;h2&gt;Confirm your purchase information&lt;/h2&gt;
                  &lt;hr&gt;
                  &lt;form method="post" name="contactform" action="mail_form.php" id="submit"&gt;
                  &lt;p&gt;
                  &lt;label for='name'&gt;Your Name:&lt;/label&gt; &lt;br&gt;
                  &lt;input type="text" name="name"&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                  &lt;label for='email'&gt;Email Address:&lt;/label&gt; &lt;br&gt;
                  &lt;input type="text" name="email"&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                  &lt;label for='purchasecode'&gt;Purchase Code:&lt;/label&gt; &lt;br&gt;
                  &lt;input type="text" name="purchasecode"&gt;
                  &lt;/p&gt;
                  &lt;p&gt;
                  &lt;label for='vendor'&gt;Vendor Name:&lt;/label&gt; &lt;br&gt;
                  &lt;select name="vendor"&gt;
                    &lt;option value="" selected="selected"&gt;&lt;/option&gt;
                    &lt;option value="Amazon" &gt;Amazon&lt;/option&gt;
                    &lt;option value="Barnes&amp;Noble" &gt;Barnes &amp;amp; Noble&lt;/option&gt;
                    &lt;option value="Family Christian" &gt;Family Christian&lt;/option&gt;
                    &lt;option value="Christianbook" &gt;Christianbook.com&lt;/option&gt;
                    &lt;option value="LifeWay" &gt;LifeWay&lt;/option&gt;
                    &lt;option value="BAM" &gt;Books-A-Million&lt;/option&gt;
                    &lt;option value="Mardel" &gt;Mardel&lt;/option&gt;
                  &lt;/select&gt;
                  &lt;/p&gt;
                  &lt;button type="submit" id="submitbutton" name="submit" value="Submit" class="mainButton"&gt;SUBMIT&lt;/button&gt;&lt;br&gt;
                  &lt;/form&gt;
  
  &lt;!--            Code for validating the form
                  Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
                  for details --&gt;
                  &lt;script type="text/javascript"&gt;
                  var frmvalidator  = new Validator("contactform");
                  frmvalidator.addValidation("name","req","Please provide your name");
                  frmvalidator.addValidation("email","email","Please enter a valid email address");
                  frmvalidator.addValidation("vendor","dontselect=000");
                  frmvalidator.addValidation("purchasecode","maxlen=50");
                  &lt;/script&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  ini_set('display_errors',1);
   error_reporting(E_ALL);
  
  if(!isset($_POST['submit']))
  {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
  }
  $name = $_POST['name'];
  $email = $_POST['email'];
  $purchasecode = $_POST['purchasecode'];
  $vendor = $_POST['vendor'];
  
  
  //Validate first
  if(empty($_POST['name'])  ||
     empty($_POST['email']) ||
     empty($_POST['purchasecode']) ||
     empty($_POST['vendor']))
  {
      echo "All fields are required.";
  exit;
  }
  
  if(IsInjected($email))
  {
      echo "Bad email value!";
      exit;
  }
  
  $email_from = $email;
  $email_subject = "GDFY Purchase Confirmation";
  $email_body = "New purchase confirmation from $name.\n".
      "Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";
  
  $to = "idc615@gmail.com";//&lt;== update the email address
  
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $email_from \r\n";
  //Send the email!
  mail($to,$email_subject,$email_body,$headers);
  //done. redirect to thank-you page.
  header('Location: index.html');
  
  // echo "success";
  
  
  // Function to validate against any email injection attempts
  function IsInjected($str)
  {
    $injections = array('(\n+)',
                '(\r+)',
                '(\t+)',
                '(%0A+)',
                '(%0D+)',
                '(%08+)',
                '(%09+)'
                );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
      {
      return true;
    }
    else
      {
      return false;
    }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>  $('#submit').submit(function() { // catch the form's submit event
        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('method'), // GET or POST
            url: $(this).attr('action'), // the file to call
            success: function(response) { // on success..
                $('#emailform').html("&lt;h2 style='text-align:center;'&gt;Thank you!&lt;/h2&gt;&lt;hr&gt;&lt;p style='text-align:center;'&gt;Thank you for submitting your purchase information.&lt;br&gt;We will send your free gifts soon!&lt;/p&gt;"); // update the DIV
            }
        });
        return false; // cancel original event to prevent form submitting
    });
  </code></pre>