Checking that a number falls in range
A range validator checks that a value sits between a lower and an upper limit. It is the right tool for quantities, ages and dates.
Setting the limits
Markup
<asp:TextBox id="txtQty" runat="server" />
<asp:RangeValidator
ControlToValidate="txtQty"
MinimumValue="1"
MaximumValue="20"
Type="Integer"
ErrorMessage="Choose between 1 and 20"
runat="server" />Two things people forget
- Set the type, or the validator compares the value as text and the result surprises you.
- Decide whether the limits themselves are allowed, because the comparison can include or exclude them.
Dates work too. Change the type to a date and the limits to dates, and the same control guards a booking window.
When the rule is a shape rather than a range, use the pattern validator.