验证逗号分隔的电子邮件列表 [英] Validate a comma separated email list
问题描述
我试图提出一个正则表达式来验证逗号分隔的电子邮件列表。
我想首先验证完整的列表,然后拆分(;),然后从拆分中修剪每个数组值(每个电子邮件)。 / p>
我想验证以下表达式:
EMAIL,EMAIL - > Ok
EMAIL,EMAIL - > Ok
EMAIL,EMAIL - > Ok
EMAIL,EMAIL - >错误
EMAIL,notAnEmail,EMAIL - >错误
我知道验证电子邮件有很多复杂的表达式,但我不需要任何花哨,这只是适用于我: /\S+@+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++像简单的JS,不是jQuery。谢谢。
编辑:我已经考虑过拳头验证然后拆分,但是我已经尝试过的表达式,这将被验证为两个正确的电子邮件: / p>
EMAIL,EMAIL。 EMAIL
我想像每个电子邮件一样验证列表本身。
一个更简单的方法是先分割字符串:
var emails = emailList.split(,);
这将创建一个数组。然后,您可以遍历数组,并检查元素是否不为空,还有一个有效的emailadres。
var valid = true;
var regex = /^(([^<>()[\]\\,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ()[\] \\;:。\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,...}))$ /。对于(var i = 0; i if(emails [i] ==||!regex.test(emails [i]),
)){
valid = false;
}
}
注意:我得到了正则表达式从这里
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+/;
I would like plain and simple JS, not jQuery. Thanks.
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.
An easier way would be to split the string first:
var emails = emailList.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.
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
这篇关于验证逗号分隔的电子邮件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!