Home > Software design >  Regex expression to validate maximum 5 email address separated by comma
Regex expression to validate maximum 5 email address separated by comma

Time:04-02

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

CodePudding user response:

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; }

CodePudding user response:

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}$";
  • Related