[英]Javascript e-mail validation of specific domains
I can't figure out what is missing so that when e-mail is valid it will skip the last invalid message and move to next item on form for validation: 我无法弄清楚丢失了什么,因此当电子邮件有效时,它将跳过最后一条无效消息,并移至表单上的下一项进行验证:
enter code here
if (document.form1.email.value.length > 0) {
var tst = document.form1.email.value;
var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }
var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
var aLst = emailRE.exec(tst)
if (!aLst) {
alert(tst + ' is not a valid e-mail')
} else {
var sLst = aLst[1].toLowerCase()
for (var i = 0; i < okd.length; i++) {
if (sLst == okd[i]) {
// alert(aLst[1] + ' is allowed');-->
}
}
if (i == okd.length) alert(aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.')
document.form1.email.select();
return false;
}
}
I'd recommend placing this code into a function, maybe named ValidateEmail()
. 我建议将此代码放入一个函数中,该函数可能名为ValidateEmail()
。
In your loop: if you've determined the email is valid, return true;
在循环中:如果您确定电子邮件有效,则return true;
否则, return true;
. 。 This will prevent further execution. 这将阻止进一步执行。 If that domain doesn't match, have it continue looping to check the others. 如果该域不匹配,请让其继续循环以检查其他域。
If the loop completes without returning true
, you'll know it didn't match anything so return false;
如果循环完成而没有返回true
,您将知道它与任何内容都不匹配,因此return false;
否则, return false;
at the very end. 在最后。
EDIT: Use try/catch instead: 编辑:改用try / catch:
if (document.form1.email.value.length > 0) {
var tst = document.form1.email.value;
var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com']
for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() }
try {
var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
var aLst = emailRE.exec(tst)
if (!aLst)
throw (tst + ' is not a valid e-mail');
// isValidDomain will be changed to 'true' only if it matches an item in the array
var isValidDomain = false;
var sLst = aLst[1].toLowerCase()
for (var i = 0; i < okd.length; i++) {
if (sLst == okd[i]) {
isValidDomain = true;
// We break here because a match has been found - no need to compare against the other domain names.
break;
}
}
if(!isValidDomain)
throw (aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.');
// If execution reaches here, you know it passed both tests!
return true;
}
catch(err) {
// This code block runs whenever an error occurs
alert(err);
document.form1.email.select();
return false;
}
}
throw
basically acts like a goto
command. throw
基本上像goto
命令一样。 It will jump directly to the catch(err)
portion of the code. 它将直接跳到代码的catch(err)
部分。
More info about try, catch, and throw: 有关尝试,捕捉和投掷的更多信息:
Thank you very much Colin! 非常感谢Colin!
I had to remove the following 2 lines to avoid halting the code from running on to next validation field: 我必须删除以下两行,以避免暂停代码继续运行到下一个验证字段:
isValidDomain = true;
// We break here because a match has been found - no need to compare against the other domain names.
// break - exits code from running on down to next item on page
}
}
if (!isValidDomain)
throw (aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.');
// If execution reaches here, you know it passed both tests!
// return true; - was not needed, stops code from running on page
}
catch (err) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.