ASP.NET CompareValidator

In this post we will see how to use compare validator to compare the two input values. Using compare validator we can compare two values from two different input controls or we can compare input value with some constant or fixed value.

Example of compare validator

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ul style="list-style-type: none">
            <li>
                <asp:TextBox ID="txt1" runat="server" />
                =
                <asp:TextBox ID="txt2" runat="server" />
            </li>
            <li>
                <asp:Button ID="Button1" Text="Validate" runat="server" />
            </li>
        </ul>
        <br>
        <asp:CompareValidator ID="compareval" Display="dynamic" ControlToValidate="txt1"
            ControlToCompare="txt2" ForeColor="red" Type="String" EnableClientScript="false"
            Text="Values are not equal." runat="server" />
    </div>
    </form>
</body>
</html>

So in compare validator we need to set ControlToValidate and ControlToCompare in order to compare two input values.

RangeValidator to validate data in between | asp.net

From the set of validation server controlin asp.net we will see how to use RangeValidator in this post.

RangeValidator is used to check if values entered are in between specific range or not. In order to use RangeValidator, we need to add server control on web form.

RangeValidator example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter a number between 0 and 5:
        <br>
        <asp:TextBox ID="txtNumber" runat="server" />
        <asp:RangeValidator ID="RangeValidator1" ControlToValidate="txtNumber" MinimumValue="0"
            MaximumValue="5" Type="Integer" Text="The number should be in between 0 to 5."
            ForeColor="red" runat="server" />
        <br>
        <asp:Button ID="Button1" Text="Submit" runat="server" />
    </div>
    </form>
</body>
</html>

In order to rangevalidator to be work we need to set few properties: 1. Minimum value
2. Maximum value
3. Control to validate
4. Type of data i.e. Interger, Date...etc.
5. And the Text for error message