gpt4 book ai didi

javascript - 如何使用正则表达式验证多封电子邮件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:55 26 4
gpt4 key购买 nike

在对 Stackoverflow 进行快速研究后,我无法找到使用正则表达式进行多电子邮件验证的任何解决方案(拆分 JS 函数不适用,但由于某些原因,应用程序的后端等待带有电子邮件的字符串用;分隔)。

要求如下:

  1. 应使用以下规则验证电子邮件:[A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za -z]{2,4}
  2. 正则表达式应该接受 ; 符号作为分隔符
  3. 邮件可以写成多行,以;结尾
  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. john@smith.com;john@smith.com 2. john@smith.com;john@smith.com; 3. john@smith.com;    john@smith.com;    jjoh@smith.com;

以下是绝对不合格的情况:

  1. john@smith.com jackob@smith.com  2. jackob@smith.com,  3. daniels@mail.com     smth@mail.com

我们将不胜感激

最佳答案

我就是这样做的(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 ;
}

关于javascript - 如何使用正则表达式验证多封电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com