Posted By

Scooter on 08/02/08


Tagged

regular expressions


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

logankoester
sumandahal
uisluu
Wardy


isValidEmail


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2008/08/isvalidemail.html

Refer to the linked blog post for a broken-down explanation of the different components of the pattern.

  1. <?php
  2. /*
  3.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Validate an e-mail address.
  12. // Supports quoted local part as well as IP address for the domain part.
  13. function isValidEmail($email)
  14. {
  15. $lengthPattern = "/^[^@]{1,64}@[^@]{1,255}$/";
  16. $syntaxPattern = "/^((([\w\+\-]+)(\.[\w\+\-]+)*)|(\"[^(\\|\")]{0,62}\"))@(([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9]{2,})|\[?([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3}\]?)$/";
  17. return ((preg_match($lengthPattern, $email) > 0) && (preg_match($syntaxPattern, $email) > 0)) ? true : false;
  18. }
  19. ?>

Report this snippet  

You need to login to post a comment.