Navarr's Tech Side The Technical Side of my Life

27Dec/105

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!

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.'
}

Posted by Navarr Barnier

Filed under: PHP Leave a comment
  • MKA

    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; }

  • http://technobabbl.es/ Voyagerfan5761

    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).

  • http://technobabbl.es/ Voyagerfan5761

    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.

  • http://tech.gtaero.net/ Navarr Barnier

    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)

  • http://tech.gtaero.net/ Navarr Barnier

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

  • Pingback: custom seat covers

  • Pingback: car mats direct