Demonstrates the CompareToValueValidator with different types of data entry controls
The CompareToValueValidator compares a field’s value to another value that you supply.
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 ID of the field in the ControlIDToEvaluate
property.
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 data type.
Use the Operator property for the comparison operator.
It supports "Equals", "NotEquals", "LessThan", "LessThanEqual", "GreaterThan", and "GreaterThatEqual".
Use
ValueToCompare for the value. It takes a string
which should reflect the same data type you specified, following the culture
format set in PeterBlum.DES.Globals.WebFormDirector.CultureInfo. Alternatively, you can programmatically
assign a native type, like an integer or DateTime, to the
ValueToCompareAsNative property and it will convert the value into a correctly
formatted string.
The ErrorMessage and SummaryErrorMessage
properties supports the token "{VALUETOCOMPARE}". It is replaced by the value in
the ValueToCompare property.
Controls
Textbox used for integer entry
Enter an integer. To be valid, it must be less than 50.
IntegerTextBox
Any of DES's data type entry textboxes (integer, decimal, date, etc) do not
require setting the
DataType property.
Enter an integer. To be valid, it must be less than 50.
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".
Must be 1
DES Calendar
While there are other features of the
Calendar control to exclude dates, this example requires
selecting only dates prior to today. In Page_Load, Today's date is assigned to
CompareToValueValidator.ValueToCompareAsNative.
When using the Calendar, the
CompareToValueValidator.DataType property can be omitted.
27 | 28 | 29 | 30 | 31 | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
1 | 2 | 3 | 4 | 5 | 6 | 7 |
Less than 11/23/2024
See also:
On DateTextBoxes
On TimeOfDayTextBox and DurationTextBox
Source Code (C#)
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CompareToValueValidator4.ValueToCompareAsNative = DateTime.Today;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PeterBlum.DES.Globals.WebFormDirector.IsValid)
{
}
}
</script>
<h2>Textbox used for integer entry</h2>
Enter an integer. To be valid, it must be less than 50.<br/><br/>
<des:TextBox ID="TextBox1" runat="server" ></des:TextBox>
<des:CompareToValueValidator ID="CompareToValueValidator1" runat="server"
ControlIDToEvaluate="TextBox1" DataType="Integer"
Operator="LessThan" ValueToCompare="50"
ErrorMessage="Less than {VALUETOCOMPARE}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:CompareToValueValidator>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator1" runat="server"
ControlIDToEvaluate="TextBox1" DataType="Integer"
ErrorMessage="Enter an integer.">
<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 an integer. To be valid, it must be less than 50.<br/><br/>
<des:IntegerTextBox ID="IntegerTextBox1" runat="server" ></des:IntegerTextBox>
<des:CompareToValueValidator ID="CompareToValueValidator2" runat="server"
ControlIDToEvaluate="IntegerTextBox1"
Operator="LessThan" ValueToCompare="50"
ErrorMessage="Less than {VALUETOCOMPARE}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:CompareToValueValidator>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator2" runat="server"
ControlIDToEvaluate="IntegerTextBox1"
ErrorMessage="Enter an integer.">
<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/><br/>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="1">Group 1</asp:ListItem>
<asp:ListItem Value="2">Group 2</asp:ListItem>
</asp:ListBox>
<des:CompareToValueValidator ID="CompareToValueValidator3" runat="server" ControlIDToEvaluate="ListBox1"
ErrorMessage="Must be 1" ValueToCompare="1" Operator="Equal" DataType="Integer" />
<br/><br/>
<h2>DES Calendar</h2>
While there are other features of the <a href="../../../DateAndTime/Menu.aspx?Topic=Calendars">Calendar</a> control to exclude dates, this example requires
selecting only dates prior to today. In Page_Load, Today's date is assigned to <span class="PropertyName">CompareToValueValidator.ValueToCompareAsNative</span>.<br/>
When using the Calendar, the <span class="PropertyName">CompareToValueValidator.DataType</span> property can be omitted.<br/><br/>
<des:Calendar ID="Calendar1" runat="server" >
<ClearCommand ShowField="RecommendedRow" />
</des:Calendar>
<des:CompareToValueValidator ID="CompareToValueValidator4" runat="server" ControlIDToEvaluate="Calendar1"
Operator="LessThan" ErrorMessage="Less than {VALUETOCOMPARE}">
</des:CompareToValueValidator>
<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>