We help IT Professionals succeed at work.

How to use String type in range validator

Dinesh Kumar
Dinesh Kumar used Ask the Experts™

I want to limit 5 character string for the password, I see that i am having maxlength property but how to use Type="String" in range validator?

        password: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="*" ControlToValidate="TextBox1" Type="String"></asp:RangeValidator>

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I always use regular expressions when validating. You can add a RegularExpressionValidator to your form and then attach the Regex to the code behind:

 <asp:RegularExpressionValidator ID="RegEx_TextBox1" ControlToValidate="TextBox1" runat="server" Display="Dynamic" ErrorMessage="You have exceeded five characters!"></asp:RegularExpressionValidator>

Open in new window

RegEx_TextBox1.ValidationExpression = @"^{0,5}$";

Open in new window

You can try using this online regex builder: http://gskinner.com/RegExr/


My question still exists i.e
How to use String type in range validator?
You can use range validators for String but it's not recommended for what you are looking to do. Following these sources for discussions on it. I personally think you should use a RegularExpressionValidator to check if someone entered a maximum if 5 characters (but that's me! :) )


Hope that helps.
Hi! When you specify Type="String" for range validator, then it will compare input string with MinimumValue and MaximumValue using string comprasion. E.g. is MinimumValue="a" and MaximumValue="bb", then "aa" is valid, but "1" and "dd" are invalid.
So I guess it would be hard to use for maximum length limit. I would advise to use CustomValidator or RegularExpressionValidator as posted above.

 The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters.

    1.The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.
    2.The validation will not fail if the input value cannot be converted to the data type specified. Use the CompareValidator control, with its Operator property set to ValidationCompareOperator.DataTypeCheck, to verify the data type of the input value.
    3.Specifies the data type of the value to check. The types are:


     4.The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue  property cannot be converted to the data type specified by the Type property.

1. RequiredFieldValidator - Checks to make sure the user entered a value.
2. CompareValidator - Compares a form field's value with the value of another form field using relations like less than, equal, not equal, etc.
3. RangeValidator - Ensures that a form field's value is within a certain range.
4. RegularExpressionValidator - Makes sure that a form field's value corresponds to a specified regular expression pattern.
5. CustomValidator - Checks the form field's value against custom validation logic that you, the developer, provide.



I included the code below as a solution:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid Length" ControlToValidate="TextBox1" ValidationExpression=".{5}"></asp:RegularExpressionValidator>

Open in new window


That means we can not do range validation using  Type="String" with RangeValidator?
That is correct. This is why we use regular expressions or a custom validator to check string length but regex is the way to go.


There must be some reason that 's why Microsoft is providing the string type for range validator.