How can I set the maximum length of a multi-line text box in a web form?

Restricting the length of text on a Multi line textbox is not supported by the standard ASP.NET (javascript:void(0)) web control. This can be done by adding the following JavaScript function in your ASPX page:

  1. Add a JavaScript. At the top of your ASPX page you will see the tag </ head > . Add the following code under this tag:

    <script language="javascript" type="text/javascript">function Count(text,long) { var maxlength = new Number(long); // Change number to your max length. if (text.value.length > maxlength){ text.value = text.value.substring(0,maxlength); alert( Only + long + chars); }
    }</script>
    
  2. Change your text controls on your ASPX page. On every multi-line text box where you would like to control the length, add the following code to the ASP text box control.

    onKeyUp=Count(this,50) onChange=Count(this,50)
    

Your original text control before adding this code:

<asp:TextBox ID=TESTDATA runat=server TextMode=MultiLine></asp:TextBox>

After adding the code:

<asp:TextBox ID=TextBox1 runat=server onKeyUp=Count(this,50) onChange=Count(this,50) TextMode=MultiLine></asp:TextBox>