Reputation: 177173
I have an asp:TextBox and I want to validate that the number of characters typed in by the user is not more than 250 characters.
Because it's a multiline TextBox the MaxLength
property does not work. At the moment I only see the option to use a CustomValidator with checking TextBox1.Text.Length
on server side and perhaps in addition some Javascript client side validation.
But isn't there a simpler way to do this, using the standard ASP.NET validators (RegularExpressionValidator, RangeValidator, CompareValidator, etc.)?
Thanks in advance!
Upvotes: 3
Views: 12863
Reputation: 49301
You have to use the RegularExpressionValidator for this. This example allows up to 1000 characters in a multiline TextBox:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" Display="dynamic"
ControlToValidate="Comments"
ValidationExpression="^([\S\s]{0,1000})$"
ErrorMessage="Please enter maxium 1000 characters for Comments">
</asp:RegularExpressionValidator>
Upvotes: 9