IntroductionIn this article, I given the full use of the regular expression validate control in asp.net. The regular expression validates control work with regex patterns. This control is most the developer used validate the following scenarios, - Email
- Phone Number
- Postal code
- Internet url
- Date
- Currency
- Custom
When we are going to use this control in page in order to perform the validations, there are two properties is most important. - ControlToValidate
- ValidationExpression
ControlToValdidateThis property is tells to validate control, you have to perform validation against to specified control value. ValidationExpressionThis property tells to validation control, performs the validation against to this expression and entered value in to be validated. Now we will perform few validations with this control. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Untitled Page</title></head><body><form id="form1" runat="server"><div><asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox><asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtEmail" runat="server" ErrorMessage="Please enter valid email address " ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></div></form></body></html> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Untitled Page</title></head><body><form id="form1" runat="server"><div><asp:TextBox ID="txtPhone" runat="server" ></asp:TextBox><asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtPhone" runat="server" ErrorMessage="Please enter valid phone number" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator></div></form></body></html><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Validator Control Overview</title></head><body><form id="form1" runat="server"><div><asp:TextBox ID="txtpostalcode" runat="server" ></asp:TextBox><asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtpostalcode" runat="server" ErrorMessage="Please enter valid postal code" ValidationExpression="\d{6}"></asp:RegularExpressionValidator></div></form></body></html><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Validator Control Overview</title></head><body><form id="form1" runat="server"><div><asp:TextBox ID="txturl" runat="server" ></asp:TextBox><asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txturl" runat="server" ErrorMessage="Please enter valid url" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator></div></form></body></html> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Validator Control Overview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtDate" runat="server" ></asp:TextBox>
<asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtDate"
runat="server" ErrorMessage="Please enter the date(12/01/2002 )" ValidationExpression="^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$"></asp:RegularExpressionValidator>
</div>
</form>
</body>
</html><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Validator Control Overview</title></head><body><form id="form1" runat="server"><div><asp:TextBox ID="txtCurrencey" runat="server" ></asp:TextBox><asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtDate" runat="server" ErrorMessage="Please enter valid format currency($0.84 | $1,234,567.89)" ValidationExpression="^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"></asp:RegularExpressionValidator></div></form></body></html>To custom validation, you can get any pattern from here as you like want to validate against your data, then specify the pattern to ValidationExpression property in Regular expression validate control and perform the validation as like you want it.thank you for reading and enjoy valid data in asp.net web site. |