views:

73

answers:

1

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!

+2  A: 

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>
Jamie Ide
It's simple and it works! Perfect! Thank you very much!
Slauma