在对 Stackoverflow 进行快速研究后,我无法找到使用正则表达式进行多电子邮件验证的任何解决方案(拆分 JS 函数不适用,但由于某种原因,应用程序后端等待一个电子邮件以 分隔的字符串);
).
以下是要求:
- 应使用以下规则验证电子邮件:
[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}(?:[;][A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}?)*
但它不适用于第#3-4点
所以这里有一些没问题的情况:
1. [email protected];[email protected]
2. [email protected];[email protected];
3. [email protected];
[email protected];
[email protected];
以下是绝对不行的情况:
1. [email protected] [email protected]
2. [email protected],
3. [email protected]
[email protected]
各种帮助将不胜感激
这就是我正在做的事情(ASP.Net 应用程序,没有 jQuery)。电子邮件地址列表在多行文本框中输入:
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 ;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)