使用JavaScript验证多封电子邮件 [英] Validate multiple emails with JavaScript
本文介绍了使用JavaScript验证多封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这两个功能:
validateEmail: function(value) {
var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return (regex.test(value)) ? true : false;
}
validateEmails: function(string) {
var self = shareEmail;
var result = string.replace(/\s/g, "").split(/,|;/);
for(var i = 0;i < result.length;i++) {
if(!self.validateEmail(result[i])) {
return false;
} else {
return true;
}
}
}
问题是,当我像这样测试电子邮件 if(!self.validateEmails(multipleEmails)){
我只根据字符串中的第一封电子邮件得到真或假,但我想测试一下字符串中的任何电子邮件。
The problem is that when I test the email like this if(!self.validateEmails(multipleEmails)) {
i get true or false based only on the first email in the string, but I want to test for any email in the string.
谢谢!
推荐答案
问题是你的if / else块;你在两种情况下都回来了。这意味着它只评估一个元素后就离开了函数。
The problem is your if/else block; You are returning under both conditions. Which means that it leaves the function after evaluating only one element.
我修改了validateEmails以证明你可能想要做的事情:
I've modified validateEmails to demonstrate what you probably want to do:
validateEmail: function(value) {
var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return (regex.test(value)) ? true : false;
}
validateEmails: function(string) {
var self = shareEmail;
var result = string.replace(/\s/g, "").split(/,|;/);
for(var i = 0;i < result.length;i++) {
if(!self.validateEmail(result[i])) {
return false;
}
}
return true;
}
这篇关于使用JavaScript验证多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文