Validate Email Address


/ Published in: PHP
Save to your folder(s)

E-mail validation is perhaps the most used validation in web forms, this code will validate email address and also optionally check the MX records of the domain provided in email address to make email validation more robust.


Copy this code and paste it in your HTML
  1. function is_valid_email($email, $test_mx = false)
  2. {
  3. if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
  4. if($test_mx)
  5. {
  6. list($username, $domain) = split("@", $email);
  7. return getmxrr($domain, $mxrecords);
  8. }
  9. else
  10. return true;
  11. else
  12. return false;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.