title: Form validation only if information is entered
body: <p>I have some client-side form validation for an email address. Essentially it checks if the email is too short, then if it's too long and finally uses <code>FILTER_VALIDATE_EMAIL</code> to check if it's valid.</p>
  
  <p>However an adjustment has been made to the form. The email field is no longer <em>required</em>. But I don't just want to remove all validation as I still want to ensure that if an email address is input it is correct. It's just not a required field any more.</p>
  
  <p>Here are the nested if statements for the validation at present:</p>
  
  <pre><code>if (isset($_POST['email_address'])){
      $email_address = mysql_real_escape_string(trim($_POST['email_address']));
      $_SESSION['status']['register']['email_address'] = $email_address;
      if(strlen($email_address) &gt; 10){ // email address less than 10
          if(strlen($email_address) &lt; 161){ // if longer than 160
              if(email_valid($email_address) == false){ // email address invalid format
                  $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
              }else{ 
                  // passed min length, passed max length, passed validation
              }
          }else {
              $_SESSION['status']['register']['error'][] = 'The email address is too long.';
          }
      } else{
          $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
      }
  }else{
      $_SESSION['status']['register']['error'][] = "You haven't put in an email address.";
  }
  </code></pre>
  
  <p>But I need to change this to do the following instead, but am having trouble getting my head around the nested statements:</p>
  
  <ol>
  <li>Check IF anything was inputted at all.
  2.If it WAS, follow the above.</li>
  <li>If it wasn't, progress anyway.</li>
  </ol>
  
  <p>So only if they actually input anything does the validation run. Else it's still OK to go forward with the form check because it wasn't required anyway. The full code can be seen here <a href="http://pastebin.com/HEFYH0Q3" rel="nofollow">pastebin.com</a> </p>