How to use CompareValidator Control in ASP.net 2.0
The CompareValidator control is used to compare data entered in two form fields. A common example we come across is comparing the Passwords while registering a new user. Another common example is while generating reports when user enters start date and end date, we need to check whether end date is always equal to or greater than the start date.
The common properties are:
ControlToValidate: We can specify the id of the control to Validate.
ControlToCompare : We can specify the id of the control to compare.
Type: Specifies the Datatype to use for comapring.
Operator: It defines the type of comparision.
Text: It specifies Error message to be displayed by the control.
The following example shows how to use the CompareValidator control to check DataType check in Csharp 2005
We use the RangeValidator control to check whether the value of a form field falls within a range, that is between a minimum and maximum value. This control can be used to check dates, numbers, currency amounts and strings. The commonly used properties of RangeValidator control are:
Type: We specify the data type to use when comparing values
Text: We specify the Error message to be displayed by the control
ControlToValidate: We specify the ID of the control that we want to validate
MaximumValue: We specify the maximum value in the range of values
MinimumValue: We specify the minimum value in the range of values
We can use the validate method of RangeValidator control to perform validation and update the IsValid property. The IsValid property of RangeValidator control has the value true when the validation check succeeds and false if not. The code below shows how to use the Validate() method of RangeValidator control.
The common properties are:
ControlToValidate: We can specify the id of the control to Validate.
ControlToCompare : We can specify the id of the control to compare.
Type: Specifies the Datatype to use for comapring.
Operator: It defines the type of comparision.
Text: It specifies Error message to be displayed by the control.
How to use the CompareValidator control in Csharp 2005
The following example shows how to use the CompareValidator control in Csharp 2005.<html> <head runat="server"> <title>Untitled Page</title> <script language="C#" runat="server"> void Button_Click(Object Sender, EventArgs e) { if (Page.IsValid == true) { lblError.Text = "Page is Valid!"; } else { lblError.Text = "The fields are empty"; } } </script> </head> <body> <form id="form1" runat="server"> <div> <b>Username</b> <br /> <asp:TextBox ID="txtUsername" Maxlength="20" CssClass="formfield" Runat="Server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtUsername" Text="Required!" Runat="Server" /> <b>Password</b> <br /> <asp:TextBox ID="txtPassword" Maxlength="20" TextMode="Password" CssClass="formfield" Runat="Server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtPassword" Text="Required!" Runat="Server" /> <br /> <b>Password (Again for confirmation)</b> <br /> <asp:TextBox ID="txtPassword2" Maxlength="20" TextMode="Password" CssClass="formfield" Runat="Server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="txtPassword2" Text="Required!" Display="Dynamic" Runat="Server" /> <asp:CompareValidator ID="CompareValidator1" ControlToValidate = "txtPassword" ControlToCompare = "txtPassword2" Type = "String" Operator="Equal" Text="Passwords must match!" Runat = "Server" /> <asp:Label ID="lblError" EnableViewState="False" runat="Server" /> <br /> <asp:Button ID="Button1" Text="Login!" OnClick="Button_Click" runat="Server" /> </div> </form> </body> </html>

How to perform DataType check using the CompareValidator control
We can check datatype using the CompareValidator control. Many a times, we need to check whether a user has enetered a date in a date filed, string in a string field, and a number in number field. For this, we use DataTypeCheck as the value for the Operator property of CompareValidator control.The following example shows how to use the CompareValidator control to check DataType check in Csharp 2005
<html> <head runat="server"> <title>Untitled Page</title> <script runat="server"> void Button_Click(Object sender, EventArgs e) { if (Page.IsValid) { lblError.Text = "Page is Valid!"; } else { lblError.Text = "Page is InValid!"; } } </script> </head> <body> <form id="form1" runat="server"> <div> Enter Currency: <asp:TextBox id="txtCurrency" Columns="10" Runat="Server"/> <asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtCurrency" Display="Dynamic" Text="Invalid Currency!" Operator="DataTypeCheck" Type="Currency" Runat="Server" /> <asp:Label ID="lblError" EnableViewState="False" runat="Server" /> <asp:Button ID="Button1" Text="Submit" OnClick="Button_Click" Runat="Server"/> </div> </form> </body> </html>
How to use RangeValidator control
We use the RangeValidator control to check whether the value of a form field falls within a range, that is between a minimum and maximum value. This control can be used to check dates, numbers, currency amounts and strings. The commonly used properties of RangeValidator control are:
Type: We specify the data type to use when comparing values
Text: We specify the Error message to be displayed by the control
ControlToValidate: We specify the ID of the control that we want to validate
MaximumValue: We specify the maximum value in the range of values
MinimumValue: We specify the minimum value in the range of values
<html> <head runat="server"> <title>Untitled Page</title> <script language="C#" runat="server"> void Check_Click(Object Src, EventArgs e) { if (Page.IsValid) { lblError.Text = "Page is Valid!"; } else { lblError.Text = "Page is InValid!" ; } } </script> </head> <body> <form id="form1" runat="server"> <asp:Label id="lblQty" Text="Stock" runat="server" /> <asp:TextBox ID="txtQty" MaxLength="15" runat="server" /> <asp:RangeValidator id="ValQty" Type="Integer" Text="Qty should be enter between 100 and 1000" ControlToValidate="txtQty" MaximumValue="1000" MinimumValue="100" runat="server"/> <asp:Label ID="lblError" EnableViewState="False" runat="Server" /> <asp:Button Text="Check" ID="btnCheck" onclick="Check_Click" runat="server" /> </form> </body> </html>
How to use Validate() method of RangeValidator control to perform validation
An alternative method to check whether a page is valid is shown below.We can use the validate method of RangeValidator control to perform validation and update the IsValid property. The IsValid property of RangeValidator control has the value true when the validation check succeeds and false if not. The code below shows how to use the Validate() method of RangeValidator control.
void Check_Click(Object Src, EventArgs e) { ValQty.Validate(); if (ValQty.IsValid) { lblError.Text = "Result: Valid!"; } else { lblError.Text = "Result: Not Valid!"; } }
Asp.net Articles

Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls