In this article I will explain with an example, how to validate Email Address using Regular Expression Validator in ASP.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net TextBox and a Button. The TextBox has been associated with the following two ASP.Net Validators.
RegularExpressionValidator:
The RegularExpressionValidator has the following properties.
1. ControlToValidate – ID of the TextBox control to be validated.
2. ValidationExpression – The Regular Expression for validating the TextBox content. In this example, the Email Address.
3. ErrorMessage – The Error Message to be displayed when Validation fails.
RequiredFieldValidator:
The RequiredFieldValidator has the following properties.
1. ControlToValidate – ID of the TextBox control to be validated.
2. ErrorMessage – The Error Message to be displayed when Validation fails.
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
ForeColor="Red" ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Display = "Dynamic" ErrorMessage = "Invalid email address"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail"
ForeColor="Red" Display = "Dynamic" ErrorMessage = "Required" />
<br />
<br />
<asp:Button Text="Submit" runat="server" />
Screenshot
Demo
Downloads