Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

CompareValidator control in asp.net - Part 46

Suggested Videos
Part 43 - Creating controls dynamically using asp.net panel control
Part 44 - RequiredFieldValidator in asp.net
Part 45 - RangeValidator in asp.net

CompareValidator control is used to compare the value of one control with the value of another control or a constant value. The comparison operation can be any of the following.
1. Equal
2. GreaterThan
3. GreaterThanEqual
4. LessThan
5. LessThanEqual
6. NotEqual
7. DataTypeCheck

CompareValidator control in asp.net



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, NotEqual etc.
4. ValueToCompare - The constant value to compare with.

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



Using CompareValidator to compare the value of one control with the value of another control.
<table>
    <tr>
        <td>
            <b>Password</b>
        </td>
        <td>
            :<asp:TextBox ID="txtPassword" runat="server" Width="150px" TextMode="Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <strong>Retype Password</strong></td>
        <td>
            :<asp:TextBox ID="txtRetypePassword" runat="server" 
                Width="150px" TextMode="Password"></asp:TextBox>
            <asp:CompareValidator ID="CompareValidatorPassword" runat="server"
            ErrorMessage="Password and Retype Password must match"
            ControlToValidate="txtRetypePassword" ControlToCompare="txtPassword"
            Type="String" Operator="Equal" ForeColor="Red">
            </asp:CompareValidator>
        </td>
    </tr>
</table>

Using CompareValidator to compare the value of one control with a constant value.
<asp:TextBox ID="txtDateofapplication" runat="server" Width="150px">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidatorDateofbirth" runat="server" 
ErrorMessage="Date of application must be greater than 01/01/2012"
ControlToValidate="txtDateofapplication" ValueToCompare="01/01/2012"
Type="Date" Operator="GreaterThan" ForeColor="Red"
SetFocusOnError="true"></asp:CompareValidator>

Using CompareValidator to check DataType
<asp:TextBox ID="txtAge" runat="server" Width="150px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorAge" runat="server" 
ErrorMessage="Age must be a number" ControlToValidate="txtAge" 
Operator="DataTypeCheck" Type="Integer" ForeColor="Red"
SetFocusOnError="true"></asp:CompareValidator>

3 comments:

  1. What is the difference between ValueToCompare and ControlToCompare property in the comparevalidator control? In the Date example, you have used ValueToCompare property, whereas in the String example (like change password) you have used ControlToCompare Property?

    ReplyDelete
    Replies
    1. ValueToCompare attribute in used when we have a default value with which we have to compare. Whereas ControlToCompare is used when we are comparing with another control

      Delete
  2. When we use ControlToCompare property as in Password and Retype password example then we have to pass for which control we want to validate as (ControlToValidate) and then to which control you want to compare your Control (ControlToCompare) as in Password example ControlToValidate="txtRetypePassword" and ControlToCompare="txtPassword"
    but incase of date example as our requirement is DateOfApply should be greater than '01/01/2012' which is a constant value so we used here ValueToCompare="01/01/2012"
    I hope you will unterstand it.................

    ReplyDelete

It would be great if you can help share these free resources