How to validate multiple emails using Regex?

后端 未结 3 1698
心在旅途
心在旅途 2021-01-13 16:02

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

3条回答
  •  无人共我
    2021-01-13 16:42

    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:

    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 ;
    }
    

提交回复
热议问题