电子邮件验证Javascript + RegEx,但排除某些域 [英] Email validation Javascript+RegEx, but to exclude certain domains
问题描述
我有客户端电子邮件验证脚本Javascript + RegEx,它可以正常工作,但是我想在验证时排除某些域,即所有Apple域,因为它们不起作用(发送到这些地址的电子邮件会被删除,恕不另行通知):@ apple.com,@ me.com,@ icloud.com,@ mac.com.
我在这里找到了适当的问题,但仍然与我要寻求帮助的问题不同.请帮忙实现这一点
可以通过RegEx修改来完成,还是在完成主要电子邮件验证后必须使用循环并搜索子字符串(@ apple.com,@ me.com,@ icloud.com,@ mac.com)?/p>
函数verifyMe(){var msg ='';var email = document.getElementById('email').value;if(!(/^ \ w +([\ .-]?\ w +)* @ \ w +([\ .-]?\ w +)*(\.\ w {2,3})+ $/.test(电子邮件))||document.getElementById('email').value ==''){msg + ='-无效的电子邮件地址:'+ email +'\ n \ n';document.getElementById('Eemail').style.color ='#ffffff';}别的document.getElementById('Eemail').style.color ='#bbb'if(味精!='')返回false;别的{search_code();//没关系继续返回true;}}
两种方法都可行.
对于正则表达式,只需在正则表达式中的 @
之后插入以下部分(负向前):
(?!(?: apple | me | icloud | mac)\.com $)
但是总体上更好的正则表达式是:
^ \ w + [-\ .. w] * @(?!(?: apple | me | icloud | mac)\.com $)\ w + [-\.\ w] *?\.\ w {2,4} $
对于另一种方法,应使用以下方法:
函数isValidMailAddress(email){var match =/^\w+[-\..w]*@(\w+[-\.\w]*?\.\w{2,4})$/.exec(email);如果(!match)返回false;var forbiddenDomains = ["apple.com","me.com","icloud.com","mac.com"];如果(forbiddenDomains.indexOf(match [1] .toLowerCase())> = 0)返回false;返回true;}
由您自己决定最适合哪种方法.
I have client side email validation script Javascript+RegEx, it works fine, but I want to exclude certain domains while validating, namely all Apple domains since they do not work (emails sent to these addresses are deleted without any notice): @apple.com, @me.com, @icloud.com, @mac.com.
I found appropriate questions here, but still they are not the same I am asking for help. Please, help to implement this
Can it be done via RegEx modification, or I have to use loop and search substrings (@apple.com, @me.com, @icloud.com, @mac.com) after the main email validation is done?
function verifyMe(){
var msg='';
var email=document.getElementById('email').value;
if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) ||
document.getElementById('email').value=='')
{
msg+='- Invalid Email Address: '+email+'\n\n';
document.getElementById('Eemail').style.color='#ffffff';
}
else
document.getElementById('Eemail').style.color='#bbb'
if(msg!='')
return false;
else
{
search_code(); //it's ok go ahead
return true;
}
}
Both approaches would work.
For the regex one, just insert the following part after the @
in the regex (negative lookahead):
(?!(?:apple|me|icloud|mac)\.com$)
But a better regex overall would be:
^\w+[-\.\w]*@(?!(?:apple|me|icloud|mac)\.com$)\w+[-\.\w]*?\.\w{2,4}$
For the other approach, the following should work:
function isValidMailAddress(email) {
var match = /^\w+[-\.\w]*@(\w+[-\.\w]*?\.\w{2,4})$/.exec(email);
if (!match)
return false;
var forbiddenDomains = ["apple.com", "me.com", "icloud.com", "mac.com"];
if (forbiddenDomains.indexOf(match[1].toLowerCase()) >= 0)
return false;
return true;
}
It's up to you to decide which approach you feel most comfortable with.
这篇关于电子邮件验证Javascript + RegEx,但排除某些域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!