I have the following ASP.NET markup:
<asp:regularexpressionvalidator id="RegularExpressionValidator7"
runat="server"
ControlToValidate="Email1"
ErrorMessage="Email not valid"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
As you can see, the validation expression is the one that Visual Web Developer gives as default. It has always worked great for me, until someone tried to enter this e-mail address:
The validator says that the email is not valid, but it is. I verified at hotmail.com and you can create an e-mail address with a hyphen just before the @ symbol.
What must I change in the validation expression to accept this hyphen?
Add an optional hyphen (
-?) before the@in your regex.Are you allowed a
+just before the hyphen too? Then you’d add it in likeand so on.