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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9780582/
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-26 07:49:40  来源:igfitidea点击:

How to validate multiple emails using Regex?

javascriptregexemail-validation

提问by John Smith

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:

以下是要求:

  1. Emails should be validated using the following rule: [A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}
  2. Regex should accept ;sign as a separator
  3. Emails can be written on multiple lines, finishing with ;
  4. Regex may accept the end of the line as ;
  1. 应使用以下规则验证电子邮件: [A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}
  2. 正则表达式应该接受;符号作为分隔符
  3. 电子邮件可以写在多行上,以 ;
  4. 正则表达式可以接受行尾为 ;

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. [email protected];[email protected]
 2. [email protected];[email protected];
 3. [email protected];
    [email protected];
    [email protected];

Here are cases that are definetely NOT OK:

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

  1. [email protected] [email protected]
  2. [email protected],
  3. [email protected]
     [email protected]

All sort of help will be appreciated

各种帮助将不胜感激

采纳答案by kirilloid

var email = "[A-Za-z0-9\._%-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,4}";
var re = new RegExp('^'+email+'(;\n*'+email+')*;?$');

[ "[email protected];[email protected]",
  "[email protected];[email protected];",
  "[email protected];\[email protected];\[email protected]",
  "[email protected] [email protected]",
  "[email protected],",
  "[email protected]\[email protected]" ].map(function(str){
    return re.test(str);
}); // [true, true, true, false, false, false]

回答by Nicholas Carey

This is how I'm doing it (ASP.Net app, no jQuery). The list of email addresses is entered in a multi-line text box:

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

回答by Bergi

There is no reason not to use split - in the same way the backend will obviously do.

没有理由不使用 split - 后端显然会这样做。

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验证电子邮件地址?.