Sunday, December 26, 2010

A “Revolutionary” Way to Validate Email Input

I’m not talking about confirming email, where you have to make sure that the
user owns an email address, but I am talking about a way to confirm that an email entered will probably work
WITHOUT succumbing to regex, AND it’s relatively quick!

[codesyntax
lang=php]
function validate_email($email)
{
$email_parts =
explode("@",$email);
if(count($email_parts)) != 2) { return FALSE; } // You can only have one @
in an email address.
$domain = $email_parts[1];
if(!getmxrr($domain,$array)) { return
FALSE; } // This domain doesn't have any MX Records.
return TRUE; // Everything else is 'valid.'
}
[/codesyntax]

7 comments:

  1. I dunno. It looks nice and neat and simple, but it requires MX lookups and doesn't take
    care of invalid characters in the user name portion of the email. After all, as it is now, validate_email("o
    hai [email protected]") and validate_email("trololol[omg](lol)w/@gmail.com") would pass.

    But
    I do like the fact that this allows you to validate the domain, something that the regex option doesn't
    offer. Add the following if before the return statement and you can be assured that the email passed it at
    least looks legit.

    if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*$", $email_parts[0])) { return
    FALSE; }

    ReplyDelete
  2. Have a look at Ian's email
    regex compendium
    . I haven't analyzed MKA's regex in detail, but at first glance it looks like it
    would reject a + in the username (which is perfectly valid).

    ReplyDelete
  3. According to the PHP docs for
    getmxrr()
    , "This function should not be used for the purposes of address verification." RFC 2821 specifies that if a domain does
    not have MX records in the DNS, the hostname itself should be used as the mail exchanger. So you can't
    declare that an address is invalid simply because there are no MX records for its domain.

    ReplyDelete
  4. I don't even understand what that means.. OH. Oh. Huh. Well then an proper email
    verification would try to connect to the server and ask if the user exists. That'd be best. XD (i don't
    think that exists in current email implementations, but will in my MSAP protocol, formerly known as reMAP)

    ReplyDelete
  5. Most email Regex does.  Its really quite disappointing when I can't signup with a + in
    my email address for websites.  Very disappointing indeed.

    ReplyDelete
  6. About the time we can make the ends meet, somebody moves the ends....

    In
    these days of austerity in addition to relative stress about incurring debt, many people balk contrary to
    the idea of making use of a credit card in order to make acquisition of merchandise or pay for any gift
    giving occasion, preferring, instead ju...

    ReplyDelete
  7. I don't think meals have any business being deductible. I'm for
    separation of calories and corporations....


    Undeniably believe that
    which you said. Your favorite justification seemed to be on the internet the easiest thing to be aware of. I
    say to you, I definitely get annoyed while people think about worries that they just do not know about. You
    managed to ...

    ReplyDelete