ASP.NET RangeValidator weirdness with MaximumValue

I have a pretty simple ASP.NET page with some input fields and validators. A single field accepting a double is as follows:

<asp:TextBox ID="tbHeight" runat="server" /> <asp:RangeValidator ID="vdHeight" runat="server" ErrorMessage="Height must be a positive number" Text="*" ControlToValidate="tbHeight" MinimumValue="0" Type="Double" /> 

This works as expected, and the user must enter a number> = 0.
Update: this does not work properly (some strange errors in the project). See comments for answers below for more details.

Then I will try the same for a field accepting an integer:

 <asp:TextBox ID="tbGrossTonnage" runat="server" /> <asp:RangeValidator ID="vdGrossTonnage" runat="server" ErrorMessage="Gross Tonnage must be a positive whole number" Text="*" ControlToValidate="tbGrossTonnage" MinimumValue="0" Type="Integer" /> 

When loading the ASP page, this gives me the following error: the value of the '' MaximumValue property for 'vdGrossTonnage' cannot be converted to an Integer type.

I do not have any specific requirements for the maximum value in the system, so I would just like the default for Int32.MaxValue (although I would have to enter 2,147,483,647, since MaximumValue does not seem to accept the Int32.MaxValue constant).

Why RangeValidator t RangeValidator type Integer not accept the missing MaximumValue property, but is it normal for one of the Double types?

+8
validation
source share
4 answers

The MinimumValue and MaximumValue properties of the MinimumValue class return string.Empty as their default value, if not set.

Further, it also happens that the protected Convert() method implemented by BaseCompareValidator , which is used to convert string properties to values, calls int.Parse() for the case of ValidationDataType.Integer . int.Parse() does not like an empty string and throws an exception.

But, in the case of ValidationDataType.Double , Convert() first calls another protected method, ConvertDouble() (instead of double.Parse() ), and in this method it explicitly returns the string value β€œ0” when an empty string is detected , and this is a string the value "0" is later parsed, via double.Parse() at 0d .

The Integer case does not use such a mapping from string.Empty to "0".

Consequently, drowsiness. The devil is in the details, and Reflector is your friend.

+15
source share

Leaving the MaximumValue field in Type = "Double" is equivalent to setting MaximumValue to 0. I’m not sure that you are correct that the first of your examples is working fine.

+1
source share

It is best to specify the MaximumValue for the RangeValidator , even if your requirements do not specifically require this. In this case, use MaxValue for Type . The default value for MaximumValue is String.Empty .

0
source share

Just to make it more confusing ..

In my view of the form (which is in the update, if that matters) I have.

 <div class="Width100PercentPadded"> <div class="Width40PercentPadded FloatLeft ClearLeft"> Latitude: </div> <div class="Width60PercentPadded FloatLeft"> <asp:TextBox runat="server" ID="txtLatitude" CssClass="Width100Percent" Text='<%# Bind("Latitude") %>' ></asp:TextBox> <asp:CompareValidator ID="CompareValidatorLatitude" runat="server" ControlToValidate="txtLatitude" Operator="DataTypeCheck" Type="Double" ErrorMessage="Latitude must be numeric" Text="must be numeric" ForeColor="Red" Display="Dynamic"></asp:CompareValidator> <asp:RangeValidator ID="RangeValidatorLatitude" runat="server" ControlToValidate="txtLatitude" MinimumValue="0" MaximumValue="90" ErrorMessage="Latitude in range 0 to 90" Text="range 0 to 90" ForeColor="Red" Display="Dynamic"></asp:RangeValidator> </div> </div> <div class="Width100PercentPadded"> <div class="Width40PercentPadded FloatLeft ClearLeft"> Longitude: </div> <div class="Width60PercentPadded FloatLeft"> <asp:TextBox runat="server" ID="txtLongitude" CssClass="Width100Percent" Text='<%# Bind("Longitude") %>'></asp:TextBox> <asp:CompareValidator ID="CompareValidatorLongitude" runat="server" ControlToValidate="txtLongitude" Operator="DataTypeCheck" Type="Double" ErrorMessage="Longitude must be numeric" Text="must be numeric" ForeColor="Red" Display="Dynamic"></asp:CompareValidator> <asp:RangeValidator ID="RangeValidatorLongitude" runat="server" ControlToValidate="txtLongitude" MinimumValue="0" MaximumValue="180" ErrorMessage="Longitude in range 0 to 180" Text="range 0 to 180" ForeColor="Red" Display="Dynamic"></asp:RangeValidator> </div> </div> 

Please note that my validation for latitude and longitude is almost the same, except that Longitude allows you to get a higher max value.

At startup, the longitude is checked exactly as expected - the type must be numeric, and the value must be between 0 and 180.

The type of the Locator must be numeric, and the range is reset if the value is negative, but the value of maxvalue is not checked. I can put millions in the text box, and the validation is not interrupted.

0
source share

All Articles