-1

I have a input text field to enter maximum of 5 email address seprated by comma, but there I should validate each email address and also validate if there less than 4 comma on that text. How do we write in single regex expression. I have done this :

[RegularExpression("^(\[\\w+-.%\]+@\[\\w.-\]+\\.\[A-Za-z\]{2,4})(,\[\\w+-.%\]+@\[\\w.-\]+\\.\[A-Za-z\]{2,4})\*$", 
ErrorMessage = "Please enter a valid email seprated by comma")\]

public string BCCEmailList { get; set; }

This will validate email address but allows user to enter any number of email seprated by comma.

answer to solve this problem

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • 2
    First, split on commas (poor choice of separator: a semi-colon would have been better). Check there are no more than five items. Then, check each one individually—referring to [C# code to validate email address](https://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address). Trying to use a regex only makes the problem bigger in this case. – Andrew Morton Apr 01 '22 at 21:25

2 Answers2

0

I found the answer while trying to combine two regular expression.

[RegularExpression("^([\\w+-.%]+@[\\w.-]+\\.[A-Za-z]{2,4})(,[\\w+-.%]+@[\\w.-]+\\.[A-Za-z]{2,4}){0,4}$", 
ErrorMessage = "Please enter maximum 5 valid email seprated by comma")]

    public string BCCEmailList { get; set; }
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Since "," isn't part of email addr - "^((\s*,\s*){0,1}[\w+-.%]+@[\w.-]+\.[A-Za-z]{2,4}){1,5}$" would do mostly same thing. – Naoyuki Tai Apr 01 '22 at 21:43
0

First you need to decide on the pattern of the email you want to use, add parantheses around it and add repeat limit afterwards, so it look like:

var myReg = @"^(\w+[\w.-_]*@[\w-]+\.\w{2,4}[,;]*){1,5}$";
Adnan
  • 186
  • 2
  • 8