Validate email field to reject certain domains

here is my current email validation code:

[INDENT][FONT=“Courier New”][B]function validateEmail(strValue) {

var objRegExp = /([1]([a-z_\.])@([a-z_\.])([.][a-z]{3})$)|([2]([a-z_\.])@([a-z_\.])(\.[a-z]{3})(\.[a-z]{2})*$)/i;

return objRegExp.test(strValue);

}[/B][/FONT][/INDENT]

what should i add to reject email addresses from certain domains like hotmaildotcom or yahoodotcom


  1. a-z ↩︎

  2. a-z ↩︎

var bad_domains = ['hotmail.com', 'yahoo.com'];
if (bad_domains.indexOf(strValue.split('@')[1]) > -1) {
  // domain is a bad one
}

Should be run after the regex validation.

Note that the indexOf method of the Array object is a part of JavaScript 1.6 and is not supported by all browsers (notably IE, including IE8).

However, a quick search for Array.indexOf can show you plenty of easy ways to get it to work cross-browser.

The reference page for Array.indexOf provides the following compatibility code, so that not-so-modern web browsers (that includes the IE range) can then use the indexOf method for arrays.


if (!Array.prototype.indexOf)  
{  
  Array.prototype.indexOf = function(elt /*, from*/)  
  {  
    var len = this.length >>> 0;  
  
    var from = Number(arguments[1]) || 0;  
    from = (from < 0)  
         ? Math.ceil(from)  
         : Math.floor(from);  
    if (from < 0)  
      from += len;  
  
    for (; from < len; from++)  
    {  
      if (from in this &&  
          this[from] === elt)  
        return from;  
    }  
    return -1;  
  };  
}