简体   繁体   English

使用JavaScript验证多封电子邮件

[英]Validate multiple emails with JavaScript

I have these two functions: 我有这两个功能:

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;
        }
    }
}

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(!self.validateEmails(multipleEmails)) {我只根据字符串中的第一封电子邮件得到真或假,但我想测试字符串中的任何电子邮件。

Thank you! 谢谢!

The problem is your if/else block; 问题是你的if / else阻止; You are returning under both conditions. 你在两种情况下都回来了。 Which means that it leaves the function after evaluating only one element. 这意味着它仅在评估一个元素后离开函数。

I've modified validateEmails to demonstrate what you probably want to do: 我修改了validateEmails来演示你可能想要做的事情:

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;
}

how about this? 这个怎么样?

validateEmails: function(string) {

    var self = shareEmail;
    var result = string.replace(/\s/g, "").split(/,|;/);
    var errors = [];
    for(var i = 0;i < result.length;i++) {
        if(!self.validateEmail(result[i])) {
            errors[i] = result[i] + ' is not valid.';
        }
     }
     if (errors.length > 0) {
        alert(errors.join('\n'));
        return false;
     } else {
         return true;
     }

}

I find the most maintainable way is to use a variable to store the return output. 我发现最可维护的方法是使用变量来存储返回输出。

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(/,|;/);
var allOk = true;


    for(var i = 0;i < result.length;i++) {
        if(!self.validateEmail(result[i])) {
            allOk = false;
        } 
    }

return allOk;
}

in case you use jquery-validate the validation method would look like: 如果您使用jquery-validate,验证方法将如下所示:

jQuery.validator.addMethod("separated_emails", function(value, element) {
    if (this.optional(element)) {
        return true;
    }

    var mails = value.split(/,|;/);
    for(var i = 0; i < mails.length; i++) {
        // taken from the jquery validation internals
        if (!/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(mails[i])) {
            return false;
        }
    }

    return true;
}, "Please specify a email address or a comma separated list of addresses");

This code below is perfect to validate multiple email addresses separated with comma or semicolon using JQuery: 下面的代码非常适合使用JQuery验证用逗号或分号分隔的多个电子邮件地址:

   var emailReg = new RegExp(/^([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6})*([,;][\s]*([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6}))*$/i);
      var emailText = $('#email').val();
      if (!emailReg.test(emailText)) {
          alert('Wrong Email Address\Addresses format! Please reEnter correct format');
            return false;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM