Return to the menu   Select another DES Module

Demonstrates the CompareTwoFieldsValidator with different types of data entry controls

The CompareTwoFieldsValidator compares a field’s value to another field's value. It is data type sensitive, converting a string to a specific data type before the comparison. (If it cannot convert, it does not report an error. Use the DataTypeCheckValidator to check for data type format errors.)

Specify the IDs of the two fields in the ControlIDToEvaluate and SecondControlIDToEvaluate properties.

Unless the data entry control is DES data type entry textbox (like DateTextBox or IntegerTextBox), always remember to set the DataType property. It supports these values: “String-Case Insensitive”, “Integer”, “Double”, “Date”, “Currency”, “Currency with Symbol”, “Positive Integer”, “Positive Double”, “Positive Currency”, and “Positive Currency with Symbol”. DES data type entry textboxes automatically supply their own datatype.

Use the Operator property for the comparison operator. It supports "Equals", "NotEquals", "LessThan", "LessThanEqual", "GreaterThan", and "GreaterThatEqual".

When entering values, you will notice a delay in showing the error message until focus as left both fields. If you want the error message to show on any focus change, set ReportsErrorsAfter to "EachEdit".


Controls

Textboxes used for integer entry

Enter integers. To be valid, the first must be less than the second.

Between and

IntegerTextBox

Any of DES's data type entry textboxes (integer, decimal, date, etc) do not require setting the DataType property.
Enter integers. To be valid, the first must be less than the second.

Between and

ListBox

The validator evaluates the ListItem.Value property, not ListItem.Text. So here it compares to "1" and "2" instead of "Group 1" and "Group 2".
This validator requires that ListBox1 and ListBox2 have different selections.



DES Calendar

The first Calendar must have a selected date less than that of the second calendar.
When using the Calendar, the CompareTwoFieldsValidator.DataType property can be omitted.

Previous Month
Next Month
September 2020
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Clear
Previous Month
Next Month
September 2020
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Clear


See also:
On DateTextBoxes
On TimeOfDayTextBox and DurationTextBox

 

Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <h2>Textboxes used for integer entry</h2> Enter integers. To be valid, the first must be less than the second.<br/><br/> Between <des:TextBox ID="TextBox1" runat="server" ></des:TextBox> and <des:TextBox ID="TextBox2" runat="server" ></des:TextBox> <des:CompareTwoFieldsValidator ID="CompareTwoFieldsValidator1" runat="server" ControlIDToEvaluate="TextBox1" SecondControlIDToEvaluate="TextBox2" DataType="Integer" Operator="LessThan" ErrorMessage="TextBox1 must be less than TextBox2"> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:CompareTwoFieldsValidator> <des:DataTypeCheckValidator ID="DataTypeCheckValidator1" runat="server" ControlIDToEvaluate="TextBox1" DataType="Integer" ErrorMessage="Enter an integer in TextBox1."> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:DataTypeCheckValidator> <des:DataTypeCheckValidator ID="DataTypeCheckValidator2" runat="server" ControlIDToEvaluate="TextBox2" DataType="Integer" ErrorMessage="Enter an integer in TextBox2."> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:DataTypeCheckValidator> <br/><br/> <h2>IntegerTextBox</h2> Any of DES's data type entry textboxes (integer, decimal, date, etc) do not require setting the <span class="PropertyName">DataType</span> property.<br/> Enter integers. To be valid, the first must be less than the second.<br/><br/> Between <des:IntegerTextBox ID="IntegerTextBox1" runat="server" ></des:IntegerTextBox> and <des:IntegerTextBox ID="IntegerTextBox2" runat="server" ></des:IntegerTextBox> <des:CompareTwoFieldsValidator ID="CompareTwoFieldsValidator2" runat="server" ControlIDToEvaluate="IntegerTextBox1" SecondControlIDToEvaluate="IntegerTextBox2" DataType="Integer" Operator="LessThan" ErrorMessage="IntegerTextBox1 must be less than IntegerTextBox2"> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:CompareTwoFieldsValidator> <des:DataTypeCheckValidator ID="DataTypeCheckValidator3" runat="server" ControlIDToEvaluate="IntegerTextBox1" ErrorMessage="Enter an integer in IntegerTextBox1."> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:DataTypeCheckValidator> <des:DataTypeCheckValidator ID="DataTypeCheckValidator4" runat="server" ControlIDToEvaluate="IntegerTextBox2" ErrorMessage="Enter an integer in IntegerTextBox2."> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:DataTypeCheckValidator> <br/><br/> <h2>ListBox</h2> The validator evaluates the <span class="PropertyName">ListItem.Value</span> property, not <span class="PropertyName">ListItem.Text</span>. So here it compares to "1" and "2" instead of "Group 1" and "Group 2".<br/> This validator requires that ListBox1 and ListBox2 have different selections.<br/><br/> <asp:ListBox ID="ListBox1" runat=server> <asp:ListItem Value="1">Group 1</asp:ListItem> <asp:ListItem Value="2">Group 2</asp:ListItem> <asp:ListItem Value="3">Group 3</asp:ListItem> <asp:ListItem Value="4">Group 4</asp:ListItem> <asp:ListItem Value="5">Group 5</asp:ListItem> <asp:ListItem Value="6">Group 6</asp:ListItem> <asp:ListItem Value="7">Group 7</asp:ListItem> </asp:ListBox> <asp:ListBox ID="ListBox2" runat=server> <asp:ListItem Value="1">Group 1</asp:ListItem> <asp:ListItem Value="2">Group 2</asp:ListItem> <asp:ListItem Value="3">Group 3</asp:ListItem> <asp:ListItem Value="4">Group 4</asp:ListItem> <asp:ListItem Value="5">Group 5</asp:ListItem> <asp:ListItem Value="6">Group 6</asp:ListItem> <asp:ListItem Value="7">Group 7</asp:ListItem> </asp:ListBox> <des:CompareTwoFieldsValidator ID="CompareTwoFieldsValidator3" runat=server ControlIDToEvaluate="ListBox1" SecondControlIDToEvaluate="ListBox2" ErrorMessage="Must not be the same" Operator="NotEqual" DataType="Integer" /> <br/><br/> <h2>DES Calendar</h2> The first <a href="../../../Menu.aspx?Topic=Calendars">Calendar</a> must have a selected date less than that of the second calendar.<br/> When using the Calendar, the <span class="PropertyName">CompareTwoFieldsValidator.DataType</span> property can be omitted.<br/><br/> <des:Calendar ID="Calendar1" runat="server" > <ClearCommand ShowField="RecommendedRow" /> </des:Calendar> <des:Calendar ID="Calendar2" runat="server"> <ClearCommand ShowField="RecommendedRow" /> </des:Calendar> <des:CompareTwoFieldsValidator ID="CompareTwoFieldsValidator4" runat="server" ControlIDToEvaluate="Calendar1" SecondControlIDToEvaluate="Calendar2" Operator="LessThan" ErrorMessage="Calendar1 must be less than Calendar2"> </des:CompareTwoFieldsValidator> <br/><br/> See also:<br/> <a href="../../../DateAndTime/DateTextBoxes/Validators.aspx" class="ControlClass">On DateTextBoxes</a><br/> <a href="../../../DateAndTime/TimeTextBoxes/Validators.aspx" class="ControlClass">On TimeOfDayTextBox and DurationTextBox</a><br/> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button> 

Return to the menu   Select another DES Module