简体   繁体   中英

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

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM