Link to home
Start Free TrialLog in
Avatar of Dinesh Kumar
Dinesh KumarFlag for India

asked on

How to use String type in range validator

Hi,

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>

thanks
dinesh
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

I always use regular expressions when validating. You can add a RegularExpressionValidator to your form and then attach the Regex to the code behind:


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

Open in new window


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

Open in new window



You can try using this online regex builder: http://gskinner.com/RegExr/
Avatar of Dinesh Kumar

ASKER

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! :) )

http://forums.asp.net/t/1046041.aspx/1
http://www.w3schools.com/aspnet/control_rangevalidator.asp

Hope that helps.
Avatar of Member_6283346
Member_6283346

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.

Note:
    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:

Currency
Date
Double
Integer
String

     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.


sources:

http://www.dreamincode.net/forums/topic/51371-check-length-of-string-using-regular-expression-regex/
http://forums.asp.net/t/1046041.aspx/1?how+to+use+RangeValidator+for+String+type+

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?
ASKER CERTIFIED SOLUTION
Avatar of bloodtrain
bloodtrain
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There must be some reason that 's why Microsoft is providing the string type for range validator.