使用RegEx验证和过滤电子邮件地址? [英] Validate and filter email address with RegEx?

查看:87
本文介绍了使用RegEx验证和过滤电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用RegEx和Javascript电子邮件字段验证来禁止免费电子邮件服务电子邮件(例如Gmail,Yahoo,Hotmail)的最佳方法是什么?

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$))$

我在这里看到了通用的用Java验证电子邮件地址解决方案使用JavaScript验证电子邮件地址吗?,但无法弄清楚如何禁止Gmail,Yahoo和Hotmail。

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


推荐答案

上千种可接受的方法之一-

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 
}

这篇关于使用RegEx验证和过滤电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆