[英]How to validate multiple emails using Regex?
After a quick research on the Stackoverflow, I wasn't able to find any solution for the multiple email validation using regex (split JS function is not applicable, but some reason back-end of the application waits for a string with emails separated by ;
). 经过对Stackoverflow的快速研究后,我无法找到任何使用正则表达式进行多重电子邮件验证的解决方案(拆分JS功能不适用,但某些原因导致应用程序的后端等待电子邮件分隔的字符串;
)。
Here are the requirements: 以下是要求:
[A-Za-z0-9\\._%-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,4}
应使用以下规则验证电子邮件: [A-Za-z0-9\\._%-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,4}
;
正则表达式应该接受;
sign as a separator 签署作为分隔符 ;
电子邮件可以写成多行,完成;
;
正则表达式可以接受该行的结尾;
I come up with this solution: 我想出了这个解决方案:
^[A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}(?:[;][A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}?)*
but it doesn't work for point #3-4 但它不适用于#3-4点
So here are cases that are OK: 所以这里的情况可以:
1. john@smith.com;john@smith.com 2. john@smith.com;john@smith.com; 3. john@smith.com; john@smith.com; jjoh@smith.com;
Here are cases that are definetely NOT OK: 以下是定义不正确的情况:
1. john@smith.com jackob@smith.com 2. jackob@smith.com, 3. daniels@mail.com smth@mail.com
All sort of help will be appreciated 各种帮助将不胜感激
This is how I'm doing it (ASP.Net app, no jQuery). 这就是我的方式(ASP.Net app,没有jQuery)。 The list of email addresses is entered in a multi-line text box: 电子邮件地址列表在多行文本框中输入:
function ValidateRecipientEmailList(source, args)
{
var rlTextBox = $get('<%= RecipientList.ClientID %>');
var recipientlist = rlTextBox.value;
var valid = 0;
var invalid = 0;
// Break the recipient list up into lines. For consistency with CLR regular i/o, we'll accept any sequence of CR and LF characters as an end-of-line marker.
// Then we iterate over the resulting array of lines
var lines = recipientlist.split( /[\r\n]+/ ) ;
for ( i = 0 ; i < lines.length ; ++i )
{
var line = lines[i] ; // pull the line from the array
// Split each line on a sequence of 1 or more whitespace, colon, semicolon or comma characters.
// Then, we iterate over the resulting array of email addresses
var recipients = line.split( /[:,; \t\v\f\r\n]+/ ) ;
for ( j = 0 ; j < recipients.length ; ++j )
{
var recipient = recipients[j] ;
if ( recipient != "" )
{
if ( recipient.match( /^([A-Za-z0-9_-]+\.)*[A-Za-z0-9_-]+\@([A-Za-z0-9_-]+\.)+[A-Za-z]{2,4}$/ ) )
{
++valid ;
}
else
{
++invalid ;
}
}
}
}
args.IsValid = ( valid > 0 && invalid == 0 ? true : false ) ;
return ;
}
var email = "[A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}";
var re = new RegExp('^'+email+'(;\\n*'+email+')*;?$');
[ "john@smith.com;john@smith.com",
"john@smith.com;john@smith.com;",
"john@smith.com;\njohn@smith.com;\njjoh@smith.com",
"john@smith.com jackob@smith.com",
"jackob@smith.com,",
"daniels@mail.com\nsmth@mail.com" ].map(function(str){
return re.test(str);
}); // [true, true, true, false, false, false]
There is no reason not to use split - in the same way the backend will obviously do. 没有理由不使用拆分 - 后端显然也会这样做。
return str.split(/;\s*/).every(function(email) {
return /.../.test(email);
}
For good or not-so-good email regular expressions have a look at Validate email address in JavaScript? 对于好的或不那么好的电子邮件,正则表达式会查看JavaScript中的验证电子邮件地址吗? . 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.