Set max length of MultiLine TextBox in ASP.Net

There was a requirement in a project where I had to set the max length of TextBox in Multiline mode so I just set the MaxLength property of TextBox to the length which was required but it doesn’t worked. So as usual I took help of search engines and found that MaxLength does not work in MultiLine mode and found two alternatives to fix the issue, one is through javascript and another through RegularExpressionValidator.

Here I am going to share the two ways which I found

  • Through Javascript:Here is the Javascript code which should be added.
    <script type="text/javascript">
         function Check(textBox, maxLength) {
              if (textBox.value.length > maxLength) {
                   alert("Max characters allowed are " + maxLength);
                   textBox.value = textBox.value.substr(0, maxLength);

    Now call the javascript function on two client side event of TextBox namely onKeyUp and onChange like this

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
    onKeyUp="javascript:Check(this, 100);" onChange="javascript:Check(this, 100);" />
  • Through RegularExpressionValidator Use "^[\s\S]{0,100}$" as Validation Expression to validate through RegularExpressionValidator like this
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ControlToValidate="TextBox1" ErrorMessage="Exceeding 100 characters" ValidationExpression="^[\s\S]{0,10}$" />

    You can change the value to in braces according to the requirement. The first value in braces is minimum required length and second value is maximum required length of Multiline TextBox.

Here is all which I found. If you get any other solution then do add your solution in your comment and help others.

All suggestions are welcome.