4

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 ;).

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 ;

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

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

John Smith
  • 41
  • 1
  • 1
  • 4
  • 2
    why is split not viable? – Joseph Mar 20 '12 at 03:21
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 – mVChr Mar 20 '12 at 03:25
  • Why is 3. not OK? According to rule 4, the regex may accept end of line as `;` and the two emails are separated by an end of line? – mathematical.coffee Mar 20 '12 at 03:27
  • (Also, could you split, validate, and then join back together to pass to the "back-end of the application"?) – mathematical.coffee Mar 20 '12 at 03:46
  • So here is the short story: For a change on JavaScript code I have to write long reports that will take 3-4 days for approval, why did I change code that 'is no my scope' – John Smith Mar 20 '12 at 04:02
  • @JohnSmith Using a convoluted expression to avoid work is going to make the next person who maintains the code really, really dislike you. –  Mar 20 '12 at 05:16
  • @Michael, really? then I should fix bug ASAP and leave my job as quick as possible – John Smith Mar 20 '12 at 05:36
  • I wonder how I manage to receive email when my email address is not an email address according to that regular expression? :( – Quentin Mar 21 '12 at 16:06

3 Answers3

2

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 ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1
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]
kirilloid
  • 14,011
  • 6
  • 38
  • 52
1

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?.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375