I'm using an asp.net Web Forms RegularExpressionValidator
Control to validate a text field to ensure it contains a series of email addresses separated by semicolons.
What is the proper regex for this task?
I'm using an asp.net Web Forms RegularExpressionValidator
Control to validate a text field to ensure it contains a series of email addresses separated by semicolons.
What is the proper regex for this task?
I think this one will work:
^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+
Breakdown:
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}
: valid email (from http://www.regular-expressions.info/)(;|$)
: either semicolon or end of string(...)+
: repeat all one or more timesMake sure you are using case-insensitive matching. Also, this pattern does not allow whitespace between emails or at the start or end of the string.
The 'proper' (aka RFC2822) regex is too complicated. Try something like (\S+@[a-zA-Z0-9-.]+(\s*;\s*|\s*\Z))+ Not perfect but should be there 90% (haven't tried it, so it might need some alteration)
Note: Not too sure about \Z it might be a Perl only thing. Try $ as well if it doesn't work.