Tuesday, March 11, 2014

Asp.NET Validation - CompareValidator

The CompareValidator control compares a value in one control with a fixed value, or, a value in another control.

Example:-
Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        First number:<br />
        <asp:TextBox runat="server" id="n1" /><br /><br />

        Last number:<br />
        <asp:TextBox runat="server" id="n2" />
        <br />
        <br />

        <asp:CompareValidator
            runat="server" id="cmpNumbers" controltovalidate="n1"
            controltocompare="n2" operator="Equal" type="Integer"
            errormessage="Both numbers are not equal!" BorderColor="#FF0066"
            BorderStyle="Solid" /><br />
    </div>
    </form>
</body>
</html>

Output:


No comments:

Post a Comment