Demonstrates the RangeValidator
The RangeValidator confirms that a field’s value is within a range of values.
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 datatype.
Use the Minimum and Maximum properties to specify the range.
These properties take strings that 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
MinimumAsNative and MaximumAsNative properties and it will convert
the value into a correctly formatted string.
The ErrorMessage and SummaryErrorMessage properties support these tokens:
- "{MINIMUM}" – The value of the Minimum property.
- "{MAXIMUM}" – The value of the Maximum property.
Controls
Textbox used for integer entry
Enter an integer. To be valid, it must be between 10 and 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 between 10 and 50.
DateTextBox
Shows setting values in
MinimumAsNative and
MaximumAsNative in Page_Load.
Enter a date. To be valid, it must be between one month ago and one month from now.
CurrencyTextBox with built-in range
CurrencyTextBox and other DES data type textboxes allow you to set the minimum and maximum on their own properties.
When done this way, the RangeValidator does not need its
Minimum and
Maximum properties specified.
Enter a number. To be valid, it must be between 10 and 20.
Source Code (C#)
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
RangeValidator3.MinimumAsNative = DateTime.Today.AddMonths(-1);
RangeValidator3.MaximumAsNative = DateTime.Today.AddMonths(1);
}
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 between 10 and 50.<br/><br/>
<des:TextBox ID="TextBox1" runat="server" ></des:TextBox>
<des:RangeValidator ID="RangeValidator1" runat="server"
ControlIDToEvaluate="TextBox1" DataType="Integer"
Minimum="10" Maximum="50"
ErrorMessage="The value must be between {MINIMUM} and {MAXIMUM}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:RangeValidator>
<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 DataType property.<br/>
Enter an integer. To be valid, it must be between 10 and 50.<br/><br/>
<des:IntegerTextBox ID="IntegerTextBox1" runat="server" ></des:IntegerTextBox>
<des:RangeValidator ID="RangeValidator2" runat="server"
ControlIDToEvaluate="IntegerTextBox1"
Minimum="10" Maximum="50"
ErrorMessage="The value must be between {MINIMUM} and {MAXIMUM}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:RangeValidator>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator2" runat="server"
ControlIDToEvaluate="IntegerTextBox1"
ErrorMessage="Enter an integer.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<br/><br/>
<h2>DateTextBox</h2>
Shows setting values in <span class="PropertyName">MinimumAsNative</span> and
<span class="PropertyName">MaximumAsNative</span> in Page_Load.<br/>
Enter a date. To be valid, it must be between one month ago and one month from now.<br/><br/>
<des:DateTextBox ID="DateTextBox1" runat="server" ></des:DateTextBox>
<des:RangeValidator ID="RangeValidator3" runat="server"
ControlIDToEvaluate="DateTextBox1"
ErrorMessage="The date must be between {MINIMUM} and {MAXIMUM}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:RangeValidator>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator3" runat="server"
ControlIDToEvaluate="DateTextBox1"
ErrorMessage="Enter a date.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<br/><br/>
<h2>CurrencyTextBox with built-in range</h2>
CurrencyTextBox and other DES data type textboxes allow you to set the minimum and maximum on their own properties.
When done this way, the RangeValidator does not need its <span class="PropertyName">Minimum</span> and
<span class="PropertyName">Maximum</span> properties specified.<br/>
Enter a number. To be valid, it must be between 10 and 20.<br/><br/>
<des:CurrencyTextBox ID="CurrencyTextBox1" runat="server" MinValue="10" MaxValue="20" ></des:CurrencyTextBox>
<des:RangeValidator ID="RangeValidator4" runat="server"
ControlIDToEvaluate="CurrencyTextBox1"
ErrorMessage="The value must be between {MINIMUM} and {MAXIMUM}.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:RangeValidator>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator4" runat="server"
ControlIDToEvaluate="CurrencyTextBox1"
ErrorMessage="Enter a number.">
<ErrorFormatterContainer>
<des:TextErrorFormatter Display="Dynamic" />
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<br/><br/>
<des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>