Validating email addresses

I’m using the jquery.validate.pack.js plugin to validate my contact form. I want to block certain email address account such as Hotmail, Outlook and Live from being validated. How can I add this rule to the validation? I have this line of code which validates the email address


```php
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$", trim($_POST['email'])))


Can I simply add the email address I want to not allow into the expression and if so where? I realise that eregi is now depreciated and I will update it.

Hi freakystreak,

Are you saying you want Hotmail address to fail validation, or you want them to pass validation without using the regex?

I want any hotmail address to fail validation

OK, so assuming that your email validation code has to return true or false, you could do it like this:


$blacklist = array(
    'hotmail.com',
    'outlook.com'
    'live.com'
);

foreach ($blacklist as $domain) {
    if (strpos($email, $domain) >= 0) {
        return FALSE;
    }
}

return (bool) preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$/i", $email);

Basically you have a blacklist of the domains that you want to fail validation, and you loop through each one checking to see if it’s present in the email address string.

Thanks for that will give it a try. Is there a way to get custom error messages with the jquery.validate.pack.js so I can let the user know that I don’t accept Hotmail? Appreciate the help