title: Validation - if statement not working
body: <p>I'm trying to use Javascript for a simple form validation. In this case, I have an email text input and I want it to error if the email is not a valid email or the email address is already taken. The valid email part is erroring fine, but the email address is already taken part always passes. </p>
  
  <p><strong>Here is my script code</strong></p>
  
  <pre><code>&lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  function valemail() 
  {
  var email = $('input#email').val();
  var atpos=email.indexOf("@");
  var dotpos=email.lastIndexOf(".");
  if (atpos&lt;1 || dotpos&lt;atpos+2 || dotpos+2&gt;=email.length)
      {
      document.getElementById("email").style.border="3px solid red";
      document.getElementById('email_error').style.display = 'block'; 
      document.getElementById('email_error').innerHTML = 'A valid email address is required';
      } else {
  if ($.trim(email) != '') 
  {
  
      $.post('../ajax/registeremail.php', {email: email}, function(data) 
      {
          if ($.trim(data) == 'That email address is already in use. If you have created an account, 
  &lt;a href="../login.php"&gt;Login&lt;/a&gt;') 
          {
              document.getElementById("email").style.border="3px solid red";
              document.getElementById('email_error').style.display = 'block'; 
              $('div#email_error').text(data);
          } else {
              document.getElementById("email").style.border="3px solid #33FF33";
              document.getElementById('email_error').style.display = 'none'; 
          }
      });
     }
  }}
   &lt;/script&gt;
  </code></pre>
  
  <p><strong>My registeremail.php file works on its own and is:</strong></p>
  
  <pre><code>&lt;?php
  include('../init.php');
  //email validation
  if (isset($_POST['email']) === true &amp;&amp; empty($_POST['email']) === false) {
  $email = $_POST['email'];
  
  if (email_exists($email) === true) {
  echo('That email address is already in use. If you have created an account, &lt;a&gt;Login&lt;/a&gt;');
  } else {
  echo('Good');
  }
  ?&gt;
  </code></pre>
  
  <p>The actual text input and div code is </p>
  
  <pre><code>      &lt;input class="input" type="text" tabindex="2" placeholder="Email*" style="height:20px;" name="email"  
  id="email" onchange="valemail()"&gt;
  &lt;div id="email_error"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Does anyone know why this is happening? I'm truly perplexed.</p>