How to validate email address using RegularExpressionValidator?

Poster
Posted by Poster under ASP.NET category on | Views : 19496
If you want your email address to be a mandatory field, write following code.

               
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="req4" runat="server" ControlToValidate="txtEmail" Text="*" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="eg. you@domain.com" ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>


If you do not want email address as mandatory field write following code.


<asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="eg. you@domain.com" ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>


This will only validate the textbox for correct email id format if something has been entered otherwise no validation will be done as there is no RequiredFieldValidator used in this code snippet.

Thanks

Comments or Responses

Posted by: Asavasamuel on: 3/18/2013 Level:Starter | Status: [Member] | Points: 10
I found this library for verifying email addresses in .net:
http://www.kellermansoftware.com/p-37-net-email-validation.aspx

Login to post response