4

What's the best way to disallow free email service emails (e.g. Gmail, Yahoo, Hotmail) using RegEx and Javascript email field validation?

^.*@(?!(aol\.com$|yahoo\.com$|hotmail\.com$))$

I've seen the generic "Validate email address in Javascript" solution here Validate email address in JavaScript? but cant figure out how to make it disallow Gmail, Yahoo and Hotmail.

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b
2
  • 1
    Do you have to use regular expressions? Could you instead just split on the @ and then compare to a blacklist of TLDs? Commented Aug 16, 2011 at 19:43
  • I'm using a 3rd party form framework that allows regular expressions for custom validation. Therefore, I have to use regular expressions. Commented Aug 23, 2011 at 18:12

1 Answer 1

8

One of thousands of acceptable ways to get there - not that yours isn't acceptable...

function isGoodEmail(email) {
    if(isValidEmail(email)) {
        if(/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
           alert(' valid email, but not for this site.  No free service emails!');
           return false;
        }
        return true;
    }
    return false;
}

function isValidEmail(email) {
  // implement using any of the email regexp's available 
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have to use regular expressions.
Sorry for the confusion. I will make sure to edit the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.