Hi Friends, in this article i will explain about the CompareValidator
in the ASP.NET. CompareValidator is used to compare the value of one input
control with value of another input control or a fixed value.
I already explained in the previous article ASP.NET Validation Controls with example | How to use Validation Controls in ASP.NET | RequiredFieldValidation Control in ASP.NET
The properties of ControlValidator 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 Data type to use for comparing like
- Currency
- Date
- Double
- Integer
- String
Operator: It defines the type of comparison like below.
- Equal
- GreaterThan
- GreaterThanEqual
- LessThan
- LessThanEqual
- NotEqual
- DataTypeCheck
Text: It
specifies Error message to be displayed by the control.
The below example shows the how to use the Compare Validator.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASPDOTNET-Roja:CompareValidator
example: how to use CompareValidator control in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="<u>P</u>assword" AccessKey="P"
AssociatedControlID="TextBox1"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
Text="*"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator" runat="server"
ControlToValidate="TextBox2"
ControlToCompare="TextBox1"
ErrorMessage="Password
does not match!">
</asp:CompareValidator>
<br />
<asp:Label ID="Label3" runat="server" Text="Re-Type Password" AssociatedControlID="TextBox2"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Compare" />
</div>
</form>
</body>
</html>
|
In the above example
TextBox2 value is compare with
TextBox1 value.If the two passwords are did not match then it will show the error like Password does not match!.
The validation is done at the onblur() of
TextBox2 .
in previous articles i explained about validation controls in ASP.NET and how to use the validation controls in ASP.NET.
CompareValidator in ASP.NET
CustomValidator Control, How to Validate with a Custom Function for ASP.NET Server Controls, ASP.NET CustomValidator Control.
RegularExpressionValidator for Email ID in ASP.NET or ASP.NET RegularExpressionValidator example.
ValidationSummary in ASP.NET
in previous articles i explained about validation controls in ASP.NET and how to use the validation controls in ASP.NET.
CompareValidator in ASP.NET
CustomValidator Control, How to Validate with a Custom Function for ASP.NET Server Controls, ASP.NET CustomValidator Control.
RegularExpressionValidator for Email ID in ASP.NET or ASP.NET RegularExpressionValidator example.
ValidationSummary in ASP.NET
No comments:
Post a Comment