Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically set the Maximum Value on a Range Validator?

Tags:

c#

validation

I want to set a Range Validator on a text box to prevent someone from ordering more of a product than is available. I have the amount available stored in a database and I databound the maximum value property of the Ranged Validator to the field in the database.

<asp:RangeValidator ID="RangeValidator1" runat="server" 
                    ControlToValidate="tbQuantity" Display="Dynamic" ErrorMessage = "Can't Order More Than Quantity." 
                    ForeColor="Red" MaximumValue='<%# Eval("Quantity") %>' MinimumValue="0"></asp:RangeValidator>

However when I debug the program I get some unexpected results.

The quantity is 17. and 1 does not trigger the error message, but 2-9 does and 10-17 doesn't trigger it but 18 and up do. I'm guessing this has something to do with the fact that it is comparing strings but I'm not sure how to change it to comparing numbers.

like image 708
Brian Green Avatar asked Aug 05 '11 21:08

Brian Green


People also ask

What is range validator control?

The RangeValidator control enables you to check whether the value of a form field falls between a certain minimum and maximum value. You must set five properties when using this control: ControlToValidate— The ID of the form field being validated. Text— The error message displayed when validation fails.

Which Validator is check the specific range?

ASP.NET RangeValidator Control. This validator evaluates the value of an input control to check that the value lies between specified ranges. It allows us to check whether the user input is between a specified upper and lower boundary.

What data types do a range validator supports?

What data types do the RangeValidator control support? This control supports five types, i.e., Currency, Date, Double, Integer and String. Integer, String, and Date.


1 Answers

The default range validator type is string, change the Type property to Integer

<asp:RangeValidator 
    ID="RangeValidator1" runat="server" 
    ControlToValidate="tbQuantity"
    Display="Dynamic"
    ErrorMessage="Can't Order More Than Quantity." 
    ForeColor="Red"
    MaximumValue='<%# Eval("Quantity") %>'
    MinimumValue="0"
    Type="Integer" /> // <-- add type property of integer

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basecomparevalidator.type.aspx

like image 144
MikeM Avatar answered Sep 18 '22 22:09

MikeM