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.


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!";
}
}



cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of asp.net book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »