0

Regular expression for allow multiple email ids separated by comma,should not allow multiple commas at the end and in between two email ids.

My regular expression for allow multiple email id's is ((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)*

But it is allowing multiple commas at the end of the email id and inbetween two email ids For ex:

 **test@abc.com,,test2@abc.com,,**

this should not be allowed

Vamsi
  • 4,237
  • 7
  • 49
  • 74
Lakki
  • 65
  • 1
  • 6
  • `fnord+@example.com` and `*@dk` are also syntactically valid email addresses. Please don't create yet another ad-hoc address filter which rejects valid addresses and permits invalid ones. – tripleee Sep 06 '12 at 13:10

3 Answers3

0

You should change the ([,])* part to ([,])? to allow 0 or 1 commas there instead of any number of commas.

tomsv
  • 7,207
  • 6
  • 55
  • 88
  • How can we specify the no's commas neede? – Lakki Sep 06 '12 at 11:54
  • Both * and ? will allow 0 matches, i.e., no commas. If you instead want exactly one comma between addresses and after the last one you just type (,) – tomsv Sep 06 '12 at 12:02
0

Try to put the character limit {1} after [,] comma case as I have put in below..

((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,]{1})*)*

Update 1

I have not much expertise in regex but this will work,

^(.+@[^\.].*\.[a-z]{2,})(;.+@[^\.].*\.[a-z]{2,})*$

Update 2

^(\s*,?\s*[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})+\s*$

Passes for

  • test@abc.com,test2@abc.com
  • test@abc.com test2@abc.com
  • test@abc.com
  • test@abc.com, test@abc.com

Reference:

Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
0

I suggest you this :

^(([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4}){1}($|\;([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4}))+)$

For better explanations (I concatenated so many suggested solutions across the web) I invite you to use this awesome tools which explains really well RegExp parts:

https://regex101.com/r/e09X7F/1

In short :

  • My value starts and ends by a mail address

OR

  • My value starts by a mail address and is followed by a mail address preceded by a ";" and ends by this couple ";mail_address".
Delphine
  • 861
  • 8
  • 21