0% found this document useful (0 votes)
59 views

Compare Validaor Example

The CompareValidator control is used to compare the value of one control to another control or constant value using comparison operators like Equal, GreaterThan, LessThan, etc. It can compare data types and validate that fields like passwords match. Properties specify the control to compare, data type, operator, and value to compare. On a button click, it checks validation and if passed, saves data, or if failed, displays an error.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Compare Validaor Example

The CompareValidator control is used to compare the value of one control to another control or constant value using comparison operators like Equal, GreaterThan, LessThan, etc. It can compare data types and validate that fields like passwords match. Properties specify the control to compare, data type, operator, and value to compare. On a button click, it checks validation and if passed, saves data, or if failed, displays an error.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

COMPARE VALIDATOR

CompareValidator Control is used to compare the value of one control with the value of another control
or constant value. The comparison operation can any of the following.

1. Equal
2. GreaterThan
3. GreaterThanEqual
4. LessThan
5. LessThanEqual
6. NotEqual
7. DataTypeCheck

Note: CompareValidator can also be used for DataType checking.

The following are the properties that are specific to the compare validator:

1. ControlToCompare: The control with which to compare.


2. Type: The DataType of the value to compare, String, Integer etc.
3. Operator: The Comparison operator. Equal, Not Equal etc.
4. ValueToCompare: The constant value to compare with.

Note: SetFocusOnError property is supported by all validation controls. If this property is set to true,
then the control will automatically receive focus, when validation fails.
Compare Validator Example

Compare Validator.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="CompareValidator.aspx.cs"
Inherits="WebApplication1.CompareValidator" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<b>Password</b>
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"
Width="150px" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Retype Password</b>
</td>
<td>
<asp:TextBox ID="txtRetypePassword"
runat="server" Width="150px" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ErrorMessage="Password Does Not Match!"
ForeColor="Red"
ControlToValidate="txtRetypePassword"
ControlToCompare="txtPassword"
Type="String" Operator="Equal"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
<b>Date of Application</b>
</td>
<td>
<asp:TextBox ID="txtDate" runat="server"
Width="150px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator2"
runat="server"
ErrorMessage="Application Date Must Be
Greater Than 01/01/2017!" ForeColor="Red" ControlToValidate="txtDate"
ValueToCompare="01/01/2017" Type="Date"
Operator="GreaterThan"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
<b>Age</b>
</td>
<td>
<asp:TextBox ID="txtAge" runat="server"
Width="150px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator3"
runat="server"
ErrorMessage="Age Must Be an Integer
Value!" ForeColor="Red"
ControlToValidate="txtAge" Type="Integer"
Operator="DataTypeCheck"

SetFocusOnError="true"></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="server"
Text="Save" Width="100px" OnClick="btnSave_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblStatus" runat="server"
Text="" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

CompareValidator.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class CompareValidator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

protected void btnSave_Click(object sender, EventArgs e)


{
if (Page.IsValid)
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = "Data has Been Successfully Stored!";
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Validation Failed, Data was not
Saved!";
}
}
}
}

You might also like