Regular Expression validator in asp.net

No.of Views2160
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  01 Jun 2010 21:06:07
Tag : ASP.NET , Validation Controls
In this article, i will explain, How we can use of the regular expression validator control to validate all important validations in asp.net
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In 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

ControlToValdidate

This property is tells to validate control, you have to perform validation against to specified control value.

ValidationExpression

This 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.

  • Email Address validation 
<%@ 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>
  • Phone Number Validation 
<%@ 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>
  • Postal Code Validation
<%@ 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>
  • Internet URL validation 
<%@ 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>
  • Date Format validation 
<%@ 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>
  • Currency validation
<%@ 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>
  • Custom Validation

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.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:DorababuDate Of Posted:6/2/2010 5:06:41 AM
Regular Expression validator in asp.net
I need a expression for validating password such that it should not allow some of the special characters like % ' " ; *. Can any one give me the expression such that it should allow special characters except the above and it should contain a numeric a small letter a capital letter
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top