Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

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.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.