Monday, 23 July 2012

ASP.NET Validation Controls with example | How to use ControlValidator in ASP.NET | ControlValidator Control in ASP.NET

                                   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!.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.