title: validate username, password and email with php
body: <p>I am working on validating username, pass and email with php. I need to be sure I get it right so nobody can bypass the login page. </p>
  
  <p>This is the values:</p>
  
  <pre><code>$email=$_POST['email'];
  $username=$_POST['uname'];
  $passwd=$_POST['pass'];
  $passwd2=$_POST['passcopy'];
  </code></pre>
  
  <p>So far I have email validation:</p>
  
  <pre><code>if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
      die(msg(0,"You haven't provided a valid email"));
  </code></pre>
  
  <p>password equal:</p>
  
  <pre><code>if ($passwd != $passwd2) {
  die(msg(0,"Passwords are not equal"));
  }
  </code></pre>
  
  <p>password length: </p>
  
  <pre><code>if ((strlen($passwd) &lt; 8) || (strlen($passwd) &gt; 16)) {
  die(msg(0,"Your password must be between 8 and 16 characters. Please type in a longer password"));
  }
  </code></pre>
  
  <p>I know I need to validate the username. I was thinking only lowercase a-z0-9 to avoid people making similar usernames?  Then password, what characters should I allow in a password?</p>
  
  <p>PS. I still don't understand preg_match and ereg. If somebody could explain this <code>"/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+</code> and make a preg_match for my username and password validation it would be very helpful. </p>