Posted By

philsown on 04/14/10


Tagged

email php validation


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thi3rry


Email Validation Function


 / Published in: PHP
 

URL: http://www.phillipharrington.com/

I used to have the strlen block and the 2 preg if blocks each separately return false throughout, but lately I'm in a single point of exit mindset.

I cannot take credit for the regexps - I borrowed them from someone who borrowed them. If the real author is known, please comment and attribute.

I did however make one edit, which was to increase the TLD portion from 3 chars max to 4 - to account for .info email addresses. One character - my big contribution!

Enjoy

  1. /**
  2.  * emailValid
  3.  * Email Validation Function
  4.  * I take an email as a string and test it against 2 regular expressions.
  5.  *
  6.  * @author Regexp Author unknown
  7.  * @author Phillip Harrington <[email protected]>
  8.  * @param string $email
  9.  * @return bool $valid
  10.  */
  11. function emailValid ($email)
  12. {
  13. $valid = false;
  14. $email = trim($email);
  15.  
  16. if(0 < strlen($email))
  17. {
  18. if(!preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/i', $email))
  19. {
  20. if(preg_match('/^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i', $email))
  21. {
  22. $valid = true;
  23. }
  24. }
  25. }
  26.  
  27. return $valid;
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: knedle on August 20, 2010

or php fce : filtervar(trim($REQUEST['email']), FILTERVALIDATEEMAIL)

You need to login to post a comment.