How to: Use RangeValidator Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've configured a RangeValidator control on a WebForm, using ASP.NET, and
have set the type to string, the max value to 100, and min value to 1,
however, the control to validate always throws an error, regardless of the
length of the string. For example...

<asp:textbox id="txtQuestion" runat="server" Width="400px" MaxLength="100"
Height="50px" TextMode="MultiLine" CssClass="text1"></asp:textbox>

<asp:RangeValidator id="RangeValidatorQuestion1" runat="server"
ControlToValidate="txtQuestion" ErrorMessage="Question length cannot exceed
100 characters." MinimumValue="1" MaximumValue="100">*</asp:RangeValidator>

<asp:Button id="btnSubmit" runat="server" Text="Button"
CausesValidation="true"></asp:Button>

Any ideas why this isn't working? Thanks.
 
Hi,

The rangevalidator control checks that an input value falls between two
values that are specified in the control. It does not take the length of the
input for the control that is being validated and then validate against that
value. One easy way that you could implement this type of functionality is
to use a custom validator control and write code that checks the
Trim().Length of the textbox. I am sure that there are many more. You may
want to look in the asp.net news groups for more implementations.

I hope this helps.
 
I've configured a RangeValidator control on a WebForm, using ASP.NET, and
have set the type to string, the max value to 100, and min value to 1,
however, the control to validate always throws an error, regardless of the
length of the string. For example...

<asp:textbox id="txtQuestion" runat="server" Width="400px" MaxLength="100"
Height="50px" TextMode="MultiLine" CssClass="text1"></asp:textbox>

<asp:RangeValidator id="RangeValidatorQuestion1" runat="server"
ControlToValidate="txtQuestion" ErrorMessage="Question length cannot exceed
100 characters." MinimumValue="1" MaximumValue="100">*</asp:RangeValidator>

<asp:Button id="btnSubmit" runat="server" Text="Button"
CausesValidation="true"></asp:Button>

Any ideas why this isn't working? Thanks.

The RangeValidator does just that, validates that the value entered is
within a certain range. It does not validate length. So when you specify
the range is "1" to "100", that means that the only allowed values are 1,
2, 3, 4, ... 99, 100.

You will either want to use a custom validator and provide your own script,
or a RegularExpressionValidator. However you have already set a max value
on the input field, so they shouldn't be able to enter more than 100
characters with that set. If by min length of 1 you are trying to make the
field required, then add a RequiredFieldValidator to your page.
 
Tom, i've been trying to figure out in other newsgroups, why the max value
doesn't work for the text box. My conclusion is simply that when the text box
is set to mulit-line, it renders as a text area control on the page and that
the maxlength attribute is no longer valid. Can you confirm this?
 
Tom, i've been trying to figure out in other newsgroups, why the max value
doesn't work for the text box. My conclusion is simply that when the text box
is set to mulit-line, it renders as a text area control on the page and that
the maxlength attribute is no longer valid. Can you confirm this?

No. As I said, there is no maxlength property for a RangeValidator.
Rather it is as you said a MaximumValue property, and it determines just
that. Is the data entered in the control, not the length of the data but
the actual data value, greater than the specified MaximumValue.
 
No. As I said, there is no maxlength property for a RangeValidator.
Rather it is as you said a MaximumValue property, and it determines just
that. Is the data entered in the control, not the length of the data but
the actual data value, greater than the specified MaximumValue.

Sorry, re-reading this I see you are not talking about range validators
anymore but only properties on the TextBox control. You are correct. When
a System.Web.UI.WebControls.TextBox has its TextMode property set to
MultiLine it renders client side as a TEXTAREA, which does not support the
MAXLENGTH property. Unfortunately the control hasn't been coded with the
smarts to disable the MaxLength property in the designer when the TextMode
property has been set to MultiLine.
 
Back
Top