Tuesday, 11 March 2014

CompareValidator in ASP.NET

In this article I will explain what CompareValidator in ASP.NET is and what are the syntax and properties.

The CompareValidator might not be the most commonly used validator of the bunch, but it's still useful in some cases. It can compare two values, for instance the values of two controls. In the next example, I will show you a small example of how it can be used.

Syntax:

<asp:CompareValidator />

Properties:

ControlToCompare: The name of the control to compare with

ControlToValidate: The id of the control to validate


Display: The display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
Error Message: The text to display in the ValidationSummary control when validation fails.

ID: A unique id for the control

Operator: The type of comparison to perform. The operators are:
  • Equal
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • NotEqual
  • DataTypeCheck
Runat: Specifies that the control is a server control. Must be set to "server"

Type: Specifies the data type of the values to compare. The types are:
  • Currency
  • Date
  • Double
  • Integer
  • String
ValueToCompare: A specified value to compare with

Example:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>         

<asp:CompareValidator ControlToCompare=" TextBox1" ControlToValidate=" TextBox2" ErrorMessage="error" runat="server" Operator="LessThan" Type="Integer" />       

1 comment:


  1. The CompareValidator might not be the most commonly used validator of the bunch, but it's still useful in some cases.
    Thanks for your valid and detailed information
    top10 dot net training institutes in chennai |
    dot net training institute in velachery

    ReplyDelete