Server Error in Application

An unhandled exception occurred during the execution of the current web request
posts - 60, comments - 76, trackbacks - 54

My Links

News

My Bookmarks

Archives

Post Categories

ASP.NET

ASP.NET TextBox MultiLine maxlength

ASP.NET textbox multiline maxlength

multiline textbox with maxlength in asp.net

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!!!

Print | posted on Thursday, December 27, 2007 11:41 PM | Filed Under [ ASP.NET unhandled exception ]

Feedback

Gravatar

# re: ASP.NET TextBox MultiLine maxlength

Excellent solution. Easy and straight forward.

Thanks,

Siamak
3/19/2008 5:36 AM | Siamak Shirzad

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 2 and type the answer here:

Powered by: