Geeks With Blogs

News
Server Error in Application An unhandled exception occurred during the execution of the current web request

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

Related Post:

Association, Aggregation, Composition object relationship

Talent vs Attitude – Harsha Bhogle

What we can learn from Babies

 

Posted on Thursday, December 27, 2007 11:41 PM ASP.NET , C# , Web Services , Design Pattern , unhandled exception | Back to top


Comments on this post: ASP.NET TextBox MultiLine maxlength

# re: ASP.NET TextBox MultiLine maxlength
Requesting Gravatar...
Excellent solution. Easy and straight forward.

Thanks,

Siamak
Left by Siamak Shirzad on Mar 19, 2008 5:36 AM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net