1

I trying to validate 5 comma separated email id in one regular expression. I currntly using below regex

^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$

This is valid for one email id.

I would like to know how I can achieve the same, any small inputs on the same is also greatly appreciated.

Thanks in advance.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
daredevil
  • 127
  • 2
  • 15
  • show some valid and not valid examples – Maciej Kozieja May 25 '17 at 08:24
  • 1
    Possible duplicate of [How to match a comma separated list of emails with regex?](https://stackoverflow.com/questions/4412725/how-to-match-a-comma-separated-list-of-emails-with-regex) – Sajal May 25 '17 at 08:26
  • 2
    are you sure your regex is for email? it seems a phone number. – Sierrodc May 25 '17 at 08:27
  • sa@gmail.com,a@gmail.com like that i can add only 5 email Id is valid. sa@gamil.com,sa@gamil.com like thai i add 6 or more email Id is invalid. – daredevil May 25 '17 at 08:29
  • 1
    You shouldn't approach validating all the email addresses at the same time, it's difficult to show to the user what's wrong. Split your data into an `Array` and then validate each email and the `Array.prototype.length` value. `'abc@bca.ab,abc@bca.cab,aaabc@bca.ab,abc@bca.ab'.split(',').map(validator);` – Hitmands May 25 '17 at 08:31
  • sorry @Sierrodc its my mistake – daredevil May 25 '17 at 08:35
  • Try it like this - `^[\w+.%-]+@[\w-.]+\.[A-Za-z]{2,4}(?:[\w+.%-]+@[\w-.]+\.[A-Za-z]{2,4}){0,4}$`. Basically: `^(?:,){0,4}$`. Note that if you put an unescaped `-` in `[\w+-.%]` it will form a range. Either escape it, or better, put at the start/end of the character class (`[-\w+.%]`). – Wiktor Stribiżew May 25 '17 at 08:39

2 Answers2

1

First of all, fix the pattern: - in between two chars inside a character class forms a range. So, the email part of your regex should be [-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4} (note the position of - in the first character class, in the second, it is OK to put it between a shorthand character class \w and the next char).

Next, to match 1 to 5 comma-separated emails, you need to match the first one, and then match 0 to 4 emails. And add anchors around the pattern to make sure the pattern matches the whole string:

^[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}(?:,[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}){0,4}$

Basically, ^<EMAIL>(?:,<EMAIL>){0,4}$:

  • ^ - start of string
  • <EMAIL> - an email pattern of yours
  • (?: - start of a non-capturing group acting as a container for a sequence of patterns:
    • , - a comma
    • <EMAIL> - an email pattern of yours
  • ){0,4} - zero to four occurrences of these sequences above
  • $ - end of string.

Another idea is to split with , and then validate:

var s = "abc@gg.com,abc2@gg.com,abc3@gg.com,abc4@gg.com,abc5@gg.com";
var re = /^[-\w+.%]+@[\w-.]+\.[A-Za-z]{2,4}$/;
var items = s.split(",");
if (items.length <= 5 && items.filter(function(x) { return re.test(x); }).length === items.length ) {
   console.log("VALID => ", items);
} else {
   console.log("INVALID!");
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-1

Below regex for javaScript you can use for multiple comma separated email id, hope this work for you

/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$/
mayur panchal
  • 265
  • 1
  • 16
  • This regex will also match [strings with trailing whitespaces and a comma](https://regex101.com/r/zGs4bl/1). It is also not optimized since `[.-]` is matching a dot or `-` linearly, while `(\.|-)` will cause backtracking. – Wiktor Stribiżew May 25 '17 at 10:49
  • And besides, it matches more than 5 comma separated emails. – Wiktor Stribiżew May 25 '17 at 10:58
  • yes i know i explain Below regex for javaScript you can use for multiple comma separated email id, hope this work for you – mayur panchal May 25 '17 at 10:58