Home > database >  I need an regular expression for validate comma separated emails
I need an regular expression for validate comma separated emails

Time:06-29

I am following Entity framework code first approach. I have following property in one of my models. I want to validate comma separated emails by this regular expression. But it is not working fine. Ex:

enter image description here As you can see it is validating here correctly.

enter image description here But it doesn't work in here

[RegularExpression(@"^(\s?[^\s,] @[^\s,] \.[^\s,] \s?,)*(\s?[^\s,] @[^\s,] \.[^\s,] )$",ErrorMessage = "Invalid Email")]
[DisplayName("Participant List")]
public string ParticipantList { get; set; }

Anyone know any regular expression that works properly, i would like to know. I am highly appreciate it.

Thank you.

CodePudding user response:

For validating multiple emails in TextBox, you can use following regular expression

^(([a-zA-Z0-9_\-\.] )@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-] \.) ))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*,\s*|\s*$))*$

CodePudding user response:

following regex matched if any email found (if an invalid email come after coma matched too)

((\s?[^\s,] @[^\s,] \.[^\s,] \s?,)*(\s?[^\s,] @[^\s,] \.[^\s,] ),?) 

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


this one matched to valid email separated by coma(not matched if any email is invalid)

^((\s?[^\s,] @[^\s,] \.[^\s,] \s?,)*(\s?[^\s,] @[^\s,] \.[^\s,] ),?) $

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

  • Related