title: E-Mail Validation Error in PHP All-in-One Form
body: <p>I was doing an exercise from a book and copied exactly what was in there (to my knowledge) yet I still can't get it to process a valid E-Mail. I've tried different regular expressions but have had no luck. I'm pretty sure I missed something stupid (and sound pretty stupid asking for this but I am new to PHP). Can anyone take a look and see if you can see what I'm missing (or doing wrong)?</p>
  <pre><code>    &lt;!DOCTYPE html&gt;
  
      &lt;html lang=&quot;en&quot;&gt;
         &lt;head&gt;
         &lt;title&gt;Contact Me&lt;/title&gt;
         &lt;/head&gt;
         &lt;header&gt;
         &lt;/header&gt;
         &lt;body&gt;
         &lt;?php
      function validateInput($data, $fieldName) {
      global $errorCount;
      if (empty($data)) {
          echo &quot;\&quot;$fieldName\&quot; is a required field.&lt;br /&gt;\n&quot;;
          ++$errorCount;
          $retval = &quot;&quot;;
      } else {
          $retval = trim($data);
          $retval = stripslashes($retval);
          }
      return($retval);
      }
      
      
  function validateEmail($data, $fieldName) {
      global $errorCount;
      if(empty($data)) {
          echo &quot;\&quot;$fieldName\&quot; is a required field.&lt;br /&gt;\n&quot;;
          ++$errorCount;
          $retval = &quot;&quot;;
      } else {
          $retval = trim($data);
          $retval = stripslashes($retval);
          $pattern = &quot;/^[\w-]+(\.[\w-]+)*@&quot; . &quot;[\w-]+(\.[\w-]+)*&quot; . &quot;(\.[[a-z]]{2,})$/i&quot;;
          if (preg_match($pattern, $retval)==0) {
              echo &quot;\&quot;$fieldName\&quot; is not a valid e-mail address.&lt;br /&gt;\n&quot;;
              ++$errorCount;
              }
          }
      return($retval);
  }
  
  function displayForm($Sender, $Email, $Subject, $Message) {
  ?&gt;
  &lt;h2 style = &quot;text-align:center&quot;&gt;Contact Me&lt;/h2&gt;
  &lt;form name=contact: action=&quot;ContactForm.php&quot; method=&quot;post&quot;&gt;
  &lt;p&gt;Your Name: &lt;input type=&quot;text&quot; name=&quot;Sender&quot; value=&quot;&lt;?php echo $Sender; ?&gt;&quot; /&gt;&lt;/p&gt;
  &lt;p&gt;Your E-mail: &lt;input type=&quot;text&quot; name=&quot;Email&quot; value=&quot;&lt;?php echo $Email; ?&gt;&quot; /&gt;&lt;/p&gt;
  &lt;p&gt;Subject: &lt;input type=&quot;text&quot; name=&quot;Subject&quot; value=&quot;&lt;?php echo $Subject; ?&gt;&quot; /&gt;&lt;/p&gt;
  &lt;p&gt;Message: &lt;br /&gt;
  &lt;textarea name=&quot;Message&quot;&gt;&lt;?php echo $Message; ?&gt;&lt;/textarea&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input type=&quot;reset&quot; value=&quot;Clear Form&quot; /&gt;&amp;nbsp;
  &amp;nbsp;&lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Send Form&quot; /&gt;&lt;/p&gt;
  &lt;/form&gt;
  &lt;?php
  }
  
  $showForm = TRUE;
  $errorCount = 0;
  $Sender = &quot;&quot;;
  $Email = &quot;&quot;;
  $Subject = &quot;&quot;;
  $Message = &quot;&quot;;
  
  if (isset($_POST['Submit'])) {
      $Sender = validateInput($_POST['Sender'], &quot;Your Name&quot;);
      $Email = validateEmail($_POST['Email'], &quot;Your Email&quot;);
      $Subject = validateInput($_POST['Subject'], &quot;Subject&quot;);
      $Message = validateInput($_POST['Message'], &quot;Message&quot;);
      if($errorCount==0)
          $showForm = FALSE;
      else
          $showForm = TRUE;
      }
      
  if ($showForm == TRUE) {
      if ($errorCount&gt;0)
          echo &quot;&lt;p&gt;Please re-enter the form information below.&lt;/p&gt;\n&quot;;
          displayForm($Sender, $Email, $Subject, $Message);
  }
  else {
      $SenderAddress = &quot;$Sender &lt;$Email&gt;&quot;;
      $Headers = &quot;From: $SenderAddress\n CC: $SenderAddress\n&quot;;
      $result = mail(&quot;greg.englar@gmail.com&quot;, $Subject, $Message, $Headers);
      if ($result)
          echo &quot;&lt;p&gt;Your message has been sent. Thank you, &quot; . $Sender . &quot;.&lt;/p&gt;\n&quot;;
      else
          echo &quot;&lt;p&gt;There was an error sending your message, &quot; . $Sender . &quot;.&lt;/p&gt;\n&quot;;
      }
  ?&gt;
  &lt;/body&gt; 
   &lt;/html&gt;
  </code></pre>
  <p>I think I have a good grasp as to what the code is doing and how it is working but I am clearly missing something. It doesn't accept any E-Mail as valid and always shows an error message. For reference the book I'm using is &quot;PHP Programming with MySQL Second Edition&quot; by Don Gosselin.</p>