javascript 验证逗号分隔的电子邮件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23777183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:32:55  来源:igfitidea点击:

Validate a comma separated email list

javascriptregexvalidationemail

提问by user2256799

I'm trying to come up with a regular expression to validate a comma separated email list.

我正在尝试提出一个正则表达式来验证逗号分隔的电子邮件列表。

I would like to validate the complete list in the first place, then split(";"), then trim each array value (each email) from the split.

我想首先验证完整列表,然后拆分(“;”),然后从拆分中修剪每个数组值(每个电子邮件)。

I would like to validate the following expressions:

我想验证以下表达式:

EMAIL,EMAIL  --> Ok
EMAIL, EMAIL  --> Ok
EMAIL , EMAIL  --> Ok
EMAIL , , EMAIL  --> Wrong
EMAIL , notAnEmail , EMAIL  --> Wrong

I know there's many complex expressions for validating emails but I don't need anything fancy, this just works for me: /\S+@\S+\.\S+/;

我知道验证电子邮件有很多复杂的表达式,但我不需要任何花哨的东西,这对我有用: /\S+@\S+\.\S+/;

I would like plain and simple JS, not jQuery. Thanks.

我想要简单明了的 JS,而不是 jQuery。谢谢。

Edit: I've already considered fist validate then split, but with the expressions I've tried so far, this will be validated as two correct emails:

编辑:我已经考虑过先验证然后拆分,但是使用到目前为止我尝试过的表达式,这将被验证为两个正确的电子邮件:

EMAIL, EMAIL  .  EMAIL

I would like to validate the list itself as much as every email.

我想和每封电子邮件一样验证列表本身。

回答by Anonymoose

An easier way would be to remove spaces and split the string first:

更简单的方法是删除空格并首先拆分字符串:

var emails = emailList.replace(/\s/g,'').split(",");

This will create an array. You can then iterate over the array and check if the element is not empty and a valid emailadres.

这将创建一个数组。然后,您可以遍历数组并检查元素是否为空且是否为有效的 emailadres。

var valid = true;
var regex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

for (var i = 0; i < emails.length; i++) {
     if( emails[i] == "" || ! regex.test(emails[i])){
         valid = false;
     }
}

note: I got the the regex from here

注意:我从这里得到了正则表达式

回答by Felipe Alarcon

Also note that you'll want to get rid of extra spaces so an extra step needs to be added as follows:

另请注意,您需要删除额外的空格,因此需要添加一个额外的步骤,如下所示:

var emailStr = "[email protected]   , [email protected], [email protected]\n";


function validateEmailList(raw){
    var emails = raw.split(',')


    var valid = true;
    var regex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    for (var i = 0; i < emails.length; i++) {
        if( emails[i] === "" || !regex.test(emails[i].replace(/\s/g, ""))){
            valid = false;
        }
    }
    return valid;
}


console.log(validateEmailList(emailStr))

By adding .replace(/\s/g, "")you make sure all spaces including new lines and tabs are removed. the outcome of the sample is true as we are getting rid of all spaces.

通过添加,.replace(/\s/g, "")您可以确保删除所有空格,包括新行和制表符。样本的结果是真实的,因为我们正在摆脱所有空间。

回答by sshashank124

I agree with @crisbeto's comment but if you are sure that this is how you want to do it, you can do it by matching the following regex:

我同意@crisbeto 的评论,但如果您确定这是您想要的方式,您可以通过匹配以下正则表达式来完成:

^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$

^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$

回答by Sunny_Sid

I am using the Regex from many years and code is same. I believe this works for every one:

我使用了多年的正则表达式,代码相同。我相信这对每个人都有效:

^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$

Be happy :)

要开心 :)