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.
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.
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 the RangeValidator control support? This control supports five types, i.e., Currency, Date, Double, Integer and String. Integer, String, and Date.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With