Reputation: 11311
I am trying to validate length of a string in a TextBox
. Controls on page defined as follows:
<asp:TextBox runat="server" ID="TB" />
<asp:RangeValidator runat="server" ID="RV"
MinimumValue="3" MaximumValue="20"
ControlToValidate="TB" Type="String" />
But when page runs there is run time error occurred
The MaximumValue 20 cannot be less than the MinimumValue 3
Upvotes: 4
Views: 5660
Reputation: 425
The syntax that worked for me correctly
<asp:RegularExpressionValidator ID="RegularExpressionValidator34" runat="server" ControlToValidate="fname"
Display="Dynamic" ErrorMessage="email is required" ForeColor="Red" SetFocusOnError="True"
ValidationExpression="(\s|.){5,10}">
Length must between (5-10) characters</asp:RegularExpressionValidator>
Upvotes: 0
Reputation: 3885
Just use TextBox's MaxLength propery. That is used to Get/Set maximum number of characters allowed in the text box.
For minimum length you'll need to use a CustomValidator. In that call a js function which checks the length of the string.
Try this:
<asp:TextBox runat="server" ID="TB" />
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="TB"
Text="The text length should be between 3 and 20"
ClientValidationFunction="clientValidate" Display="Dynamic">
</asp:CustomValidator>
<script type="text/javascript">
function clientValidate(sender, args) {
if (args.Value.length < 3 ||args.Value.length > 20) {
args.IsValid = false;
}
}
Upvotes: 4
Reputation: 14771
You can not validate the length of a TextBox
using a RangeValidator
!! RangeValidator
is used to validate the value of a field, not the length of this value.
To do that you can use other ways as CustomValidator
.
Upvotes: 3
Reputation: 52241
You mention Type incorrect
, it should be Type="Integer"
instead Type="String"
Upvotes: 13