Jordan Foreman
Jordan Foreman

Reputation: 3888

asp.net range validator on textbox

I have an asp:textbox with both required and range validators attached to it, where the code looks like this:

ASP:

<asp:TextBox ID="textBox1" runat="server" CausesValidation="true"></asp:TextBox>
<asp:RangeValidator ID="rangeValidator1" runat="server" ControlToValidate="textBox1" MaximumValue="1" MinimumValue="0"
     ValidationGroup="valid" ForeColor="Red" ErrorMessage="Out of Range" />
<asp:RequiredFieldValidator ID="requiredValidator1" runat="server" ControlToValidate="textBox1"
     ValidationGroup="valid" ForeColor="Red" ErrorMessage="Cannot be blank" />

And when the page is dynamically loaded (after a quick callback), I have code that is supposed to change the MaximumValue of the RangeValidator to more specific value. Here is the code for that:

rangeValidator1.MaximumValue = GetMaxValue(params).ToString();

Now, I have set a breakpoint, and rangeValidator1.MaximumValue is being set correctly, however, when the page loads, and I look at the compiled client side javascript, it appears that the maximum value is still only 1.

What confuses me more is that any integer typed in will pass, as long as the first digit is a '1'. So if the maxValue is supposed to be something like "1234567", "1" will match, as will "12345678910". But "2" will not. Nor will "3000" or "46000".

Has anyone else had a similar issue with RangeValidators on Textboxes?

Upvotes: 15

Views: 38562

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40149

The RangeValidator handles validation for multiple types. You should make sure to set the Type to Integer. or what ever is appropriate.

Upvotes: 21

Related Questions