Reputation: 235
I have the following regular expression to validate a single email address. How do I make this regex accept a comma delimited list of email addresses?
^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$
I am placing the regex within an input tag using a razor view:
<input data-val="true" data-val-length="Email field exceeds maximum length of 50" data-val-length-max="50" data-val-regex="Email-Address is invalid" data-val-regex-pattern="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" id="Email" name="Email" type="text" value="">
Upvotes: 2
Views: 240
Reputation:
If you just have interior optional comma separated list, its usually done like
(?:my regex)(?:,my regex)*
# ^(?:[A-Za-z0-9]+[_.+-]+)*[A-Za-z0-9]+@(?:\w+[.-]+)*\w{1,63}\.[a-zA-Z]{2,6}(?:,(?:[A-Za-z0-9]+[_.+-]+)*[A-Za-z0-9]+@(?:\w+[.-]+)*\w{1,63}\.[a-zA-Z]{2,6})*$
^
(?: [A-Za-z0-9]+ [_.+-]+ )*
[A-Za-z0-9]+
@
(?: \w+ [.-]+ )*
\w{1,63} \. [a-zA-Z]{2,6}
(?:
,
(?: [A-Za-z0-9]+ [_.+-]+ )*
[A-Za-z0-9]+
@
(?: \w+ [.-]+ )*
\w{1,63} \. [a-zA-Z]{2,6}
)*
$
Upvotes: 2