Validate and filter email address with RegEx?

南楼画角 提交于 2020-01-14 14:01:27

问题


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

回答1:


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 
}


来源:https://stackoverflow.com/questions/7083937/validate-and-filter-email-address-with-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!