ASP.NET CompareValidator is used to Compare Age field value of one input control with value of another input control. The CompareValidator control belongs to System.Web.UI.WebControls
Syntax
<asp:CompareValidator id="ageCheck" runat="server"
Operator="GreaterThan"
Type="Integer"
ControlToValidate="txtAge"
ValueToCompare="25"
ErrorMessage="Must be 25 years or older"></asp:CompareValidator>
Property | Description |
---|---|
ControlToValidate | Specifies control name which needs to be validate. |
Display | Specifies behavior of the error message. |
EnableClientScript | Specifies whether or not client-side validation is enabled. |
Enabled | Specifies whether or not validation control is enabled or not. |
ErrorMessage | Specifies error messageto be display in validation summary. |
ForeColor | Specifies error message color. |
IsValid | Indicate if associate control passes validation or not. |
SetFocusOnError | Set focus on associate control if validation fails. |
Text | Specifies text to be display if validation fails. |
ValidationGroup | Specifies validation group name. |
Validate | Update the isvalid propery. |
ValidateGetValidationProperty | Determineing validation property of a control, if it exists. |
Equal | This is used to check whether the compared value are equal. |
NotEqual | This is used to check whether the compared value are not equal. |
GreaterThan | This is used to check whether the compared value are greater than. |
GreaterThanEqual | This is used to check whether the compared value are greater than equal |
LessThan | This is used to check whether the compared value are less than. |
LessThanEqual | This is used to check whether the compared value are less than equal. |
DataTypeCheck | This is used to compare data type. |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Compare Age with CompareValidator Example</title>
</head>
<body>
<form id="Form" runat="server">
<div>
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium"
Font-Underline="True" Text="ASP.NET Check Age with CompareValidator Example"></asp:Label>
<table border="0" cellpadding="4" cellspacing="4" width="420px;">
<tr><td><asp:Label ID="lblNumber1" runat="server" Font-Bold="True" Font-Size="Small"
Text="Enter Age:"></asp:Label></td>
<td><asp:TextBox ID="txtAge" runat="server" Width="173px"></asp:TextBox>
<asp:CompareValidator id="ageCheck" runat="server"
Operator="GreaterThan"
Type="Integer"
ControlToValidate="txtAge"
ValueToCompare="25"
ErrorMessage="Must be 25 years or older" /> </td></tr>
<tr><td> </td><td> <asp:Button ID="btnSubmit" runat="server" Font-Bold="True"
Font-Size="Small" Text="Submit" onclick="btnSubmit_Click" /> </td></tr>
</table>
</div>
</form>
</body>
</html>
Above example will produce following output