Posted By

frederichoule on 04/01/11


Tagged

validation e-mail address


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

frederichoule
tolka
bennettstone


E-Mail Address Validation


 / Published in: PHP
 

  1. function validEmail($email) {
  2. $isValid = true;
  3. $atIndex = strrpos($email, "@");
  4. if (is_bool($atIndex) && !$atIndex) {
  5. $isValid = false;
  6. } else {
  7. $domain = substr($email, $atIndex+1);
  8. $local = substr($email, 0, $atIndex);
  9. $localLen = strlen($local);
  10. $domainLen = strlen($domain);
  11. if ($localLen < 1 || $localLen > 64) {
  12. $isValid = false;
  13. } else if ($domainLen < 1 || $domainLen > 255) {
  14. $isValid = false;
  15. } else if ($local[0] == '.' || $local[$localLen-1] == '.') {
  16. $isValid = false;
  17. } else if (preg_match('/\\.\\./', $local)) {
  18. $isValid = false;
  19. } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  20. $isValid = false;
  21. } else if (preg_match('/\\.\\./', $domain)) {
  22. $isValid = false;
  23. } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))) {
  24. if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))) {
  25. $isValid = false;
  26. }
  27. }
  28. if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
  29. $isValid = false;
  30. }
  31. }
  32. return $isValid;
  33. }

Report this snippet  

You need to login to post a comment.