« Previous entry | Next entry » Browse > Snippets

Skip to comments (8) Email Validation Revision
Posted by predominant on Oct 23 2005 @ 21:53  :: 4300 unique visits

This PHP Function will completely check an input email address to check for visual correctness. It will return TRUE for success, and FALSE for failure. This one works very well, and is very compact. 4 lines of code in the function!

CODE: PHP
function emailFormatIsValid($_email)
{
     // return TRUE for success.
     // return FALSE for failure.

     //// 'atom' is a single element before the '@' in a email address
     $atom = '[-a-z0-9!#$%&'*+/=?^_`{|}~]';

     //// '
domain' describes the text of a domain name, and it
     //// supports subdomains
     $domain = '
([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

     //// '
regex' builds a complete regular expression from the
     //// previous defined regular expressions. It adds restrictions
     //// such as domain name length.
     $regex = '
^' . $atom . '+' . '(.' . $atom . '+)*'. '@' . '(' . $domain . '{1,63}.)+' . $domain . '{2,63}' . '$';

     return (eregi($regex, $_email) ? TRUE : FALSE);
}

8 comments posted so far
Add your own »

1. On Oct 24 2005 @ 00:13 guest wrote:

If it works very well, why is it under Bad Code?

2. On Oct 24 2005 @ 00:57 guest wrote:

Probably wrong category considering the description depicts this code as done very well.

3. On Oct 24 2005 @ 10:15 TheJohnDoe2005 wrote:

The Orginal Function can be found on php.net under the function "eregi"

Here is the piece of code.

Other References
rfc2822
rfc1035

Orginally coded by : bobocop at bobocop.cz on 02-May-2005 11:09
Code taken from    : http://www.php.net

CODE: PHP
<?php
// Completely update for match RFC 2822 and RFC 1035
// http://www.faqs.org/rfcs/rfc2822.html
// http://www.faqs.org/rfcs/rfc1035.html

// Example results:

$email[] = 'foo@example.com';                      // matched
$email[] = 'foo.bar@example.co.uk';                // matched
$email[] = 'foo_bar@example.com';                  // matched
$email[] = '_foo_bar@example.com';                // matched
$email[] = 'foo@example.example';                  // matched
$email[] = '%#a+f.*&654_-._@ee.xx';                // matched
$email[] = 'foo@abc-123.xx';                      // matched
$email[] = 'a@a.a.a.a.aa';                        // matched
$email[] = 'a@a9.aa';                              // matched
$email[] = 'a!b#c$d%e^f&g*h'i+j-k{l|m}n_/@op.qr'; //matched

$email[] = '
';                                    //separator

$email[] = '
foo@-example.com';                    // not matched
$email[] = '
foo@example-.com';                    // not matched
$email[] = '
%#af.*&@a%#b.xx';                      // not matched
$email[] = 'a@a.99.00.a.aa';                      // not matched
$email[] = '_-._@-.--';                            // not matched
$email[] = 'any..thing@bla.bla';                  // not matched
$email[] = '@.';                                  // not matched
$email[] = '@.com';                                // not matched
$email[] = '@exam@exam.com';                      // not matched
$email[] = ' @ .com';                              // not matched
$email[] = '.bar@example.com';                    // not matched
$email[] = 'foo.@example.com';                    // not matched
$email[] = 'foo@example.x';                        // not matched

$atom = '[-a-z0-9!#$%&'*+/=?^_`{|}~]';    // allowed characters for part before "at" character
$domain = '
([a-z]([-a-z0-9]*[a-z0-9]+)?)'; // allowed characters for part after "at" character

$regex = '
^' . $atom . '+' .        // One or more atom characters.
'
(.' . $atom . '+)*'.              // Followed by zero or more dot separated sets of one or more atom characters.
'
@'.                                // Followed by an "at" character.
'
(' . $domain . '{1,63}.)+'.        // Followed by one or max 63 domain characters (dot separated).
$domain . '
{2,63}'.                  // Must be followed by one set consisting a period of two
'
$';                                // or max 63 domain characters.

foreach ($email as $example) {
   if (strlen($example) == 0):
       echo '
 <br>';
   else:
     if (eregi($regex, $example)):
       echo $example . '
matched<br>';
     else:
       echo '
<strong>'. $example . ' not matched</strong><br>';
     endif;
   endif;
}
?>

4. On Oct 24 2005 @ 10:30 Erik wrote:

Moved it to the snippets section.

5. On Oct 24 2005 @ 18:19 TheJohnDoe2005 wrote:

In Reality is there an email address with the following?

i.e.
Email Address    : !@gone.com or even !#$%&+/=?^_`{|}~@gone.com
Function Returns : Both return True

Comment
Although the above function somehow is compliance with the RFC. It is always
best to use something that is more practical in the real world. Maybe that is
why the author categorized it under bad code because you might not see the someone
actually using the the email address. (!@gone.com).

Check this few links out to improve the function.....
Link 1 : Remote.org
Link 2 : RFC 2821

4.5.3.1 Size limits and minimums - taken from RFC 2821

There are several objects that have required minimum/maximum sizes.
Every implementation MUST be able to receive objects of at least
these sizes.  Objects larger than these sizes SHOULD be avoided when
possible.  However, some Internet mail constructs such as encoded
X.400 addresses [16] will often require larger objects: clients MAY
attempt to transmit these, but MUST be prepared for a server to
reject them if they cannot be handled by it.  To the maximum extent
possible, implementation techniques which impose no limits on the
length of these objects should be used.

i.e. local-part@domain

local-part
The maximum total length of a user name or other local-part is 64
characters.

domain
The maximum total length of a domain name or number is 255
characters.



Characters in the local part of a mail address - taken from Remote.org

"!"
This character is used in UUCP style addresses, in the so called bang paths.
Example: host2 ! host1 ! user. Because there are still lots of mail systems
who interpret this chars specially it should never be used in the local part
of an email address.

"%"
The percent sign is still in use for source routing in some systems. Although
it is possible to use it in the local part of an email address, it might be interpreted
specially by some email system. If it is used in a <mailto:> URL, it must be URL encoded.

6. On May 10 2006 @ 16:14 lovely wrote:

hello

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: