An unhandled exception occurred during the execution

ASP.NET TextBox.MultiLine maxlength

Posted by maheshsingh21 on December 28, 2007

ASP.NET TextBox.MultiLine maxlength

According to msdn in ASP.NET you can not use textbox’s MaxLength property with Multiline Mode. But I was required in my current application. So I search google and other search engines and finally got few solution. I am simply compiling all of the solutions in single post.
Solution Number 1 for

ASP.NET TextBox.MultiLine maxlength

Add following javascript

function Count(text,long)

 

{

 

      var maxlength = new Number(long); // Change number to your max length.

 

if(document.getElementById(‘<%=textBox.ClientID%>’).value.length > maxlength){

 

            text.value = text.value.substring(0,maxlength);

 

            alert(” Only “ + long + ” chars”);

 

}

 

Where “textBox” is the asp text box ID.
Also add following events in your textbox.

onKeyUp=”javascript:Count(this,200);” onChange=”javascript:Count(this,200);”


Your textbox code should look like

 

<asp:TextBox ID=”textBox” onKeyUp=”javascript:Count(this,2);” onChange=”javascript:Count(this,2);”  TextMode=MultiLine Columns=”5″ Rows=”5″ runat=server>

    </asp:TextBox>

 

Solution Number 2 for

ASP.NET TextBox.MultiLine maxlength

Another way to achieve this is regular expression. You can add following regular expression validate on asp text box.

<asp:RegularExpressionValidator ID=”txtConclusionValidator1″ ControlToValidate=”textBox” Text=”Exceeding 200 characters” ValidationExpression=”^[\s\S]{0,2}$” runat=”server” />

Hope this helps you!!!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>