How to validate the maximum allowed number of characters in a multiline asp:TextBox?

11,800

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>
Share:
11,800
Slauma
Author by

Slauma

Updated on July 29, 2022

Comments

  • Slauma
    Slauma almost 2 years

    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!