Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:CompareValidator control
CompareValidator control is used to compare two values. The value to compare can be either a value of another control or a constant specified.
 
CompareValidator control is used to comapre two values. The value to compare can be either a value of another control or a constant specified. There are predefined data types that can be compared like string, integer etc.

 

Following are main properties of the validation control.
ControlToCompare Gets or sets the ID of the control whose value will be compared with the currently entered value.
Operator DataTypeCheck/Equal/GreaterThan/GreaterThanEqual/LessThan/LessThanEqual/NotEqual. Used to specify the comparison operation to peform. In case of DataTypeCheck, ControlToCompare properties are ingnored.
Display Dynamic/Static. Used to indicate how the area of error message will be allocated.
Dynamic: Error message area will only be allocated when error will be displayed. Static: Error messagea area will be allocated in either case.
Enabled true/false. Gets or sets whether to enable the validation control or not.
ErrorMessage Gets or sets the text of the error message that will be displayed when validation fails (This is displayed when ValidationSummary validatoin control is used.).
Text Gets or sets the description of the error message text.
ValidationGroup Gets or sets the validation group it belongs to. This is used to group a set of controls.
SetFocusOnError true/false. Used to move focus on to the control that fails the validation.
DEMO : CompareValidator Show Source Code
     
    
<asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write into TextBox"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="Server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="1st TextBox is Mandatory field" Text="<br>Please write something in 1st  Box."></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="Server" ControlToValidate="TextBox2" ErrorMessage="2nd TextBox is Mandatory field" Text="<br>Please write something in 2nd Box."></asp:RequiredFieldValidator>
    <asp:CompareValidator ID="CompareValidator1" runat="Server" ControlToValidate="TextBox2" ControlToCompare="TextBox1" Operator="Equal" Type="string" Text="Both textbox value should be same." ErrorMessage="Both textbox values are not equal." Display="Dynamic"></asp:CompareValidator>
    
    <asp:Button ID="btnSubmit" runat="Server" OnClick="WriteTextBoxValue" Text="Submit" />                
    <asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" />