Email Validation

7:07:00 PM | ,

Here is a nice little php function that will validate email addresses which is ideal for forms / registration on your website. The function provides an optional parameter to check the email against MX records for the domain in the email address.
1<?php
2function is_valid_email($email$test_mx = false)
3{
4    if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"$email))
5        if($test_mx)
6        {
7           list($username$domain) = split("@"$email);
8           return getmxrr($domain$mxrecords);
9        }
10        else
11           return true;
12     else
13        return false;
14}
15?>