Tips/Tricks – How to control MaxLength of Multiline TextBox in ASP.NET 1.1? 28

Problem : The users can enter as much charaters as he wants in Multiline Textbox even MaxLength of this textbox is set to a particular value. Obviously, this is a bug of ASP.NET.

Details of problem : When the TextMode property of textbox is set to Multiline, the MaxLength property wont be handled in controlling the maximum length of users inputs.


Demo Download

Steps to reproduce this issue (for those who doesn’t know that problem)

  1. Download Sample file
  2. Set “Problem.aspx” page as a startup page (There are two textboxes in this page. One is Multiline textbox (TextBox1.TextMode = Multiline) and another textbox is singleline textbox (TextBox2.TextMode = SingleLine). The MaxLength of Both textboxes are 10. )
  3. Run the project
  4. Type something more than 10 characters in both textboxes
    Observe: You can type as many characters as you wants in TextBox1 (1st Textbox) but not in Textbox2 (2nd Textbox)

Solution ~

This problem can be solved by using Javascript to track how many characters typed in textbox. If the length of characters are over limited number (max number) then we can simply ignore what the user type by returning “false” in javascript function.

Step #1. Put the following function in your page.

<script language="javascript">
function textboxMultilineMaxNumber(txt,maxLen){
if(txt.value.length > (maxLen-1))return false;

Step #2. Put the JS function in onkeypress event of Textbox.

&lt;asp:textbox id="TextBox1" runat="server" Width="232px" TextMode="MultiLine" Height="48px" style="Z-INDEX: 104; LEFT: 136px; TOP: 336px"
onkeypress=<strong>"return textboxMultilineMaxNumber(this,10)"</strong>></asp:textbox>

Run the sample

  1. Set “Solution.aspx” as startpage in the demo project that you just download.
  2. Run the project
  3. Type something more than 10 characters
    Observe: Both textboxes won’t let you type if the characters that you type are more than 10 as its defined.

28 thoughts on “Tips/Tricks – How to control MaxLength of Multiline TextBox in ASP.NET 1.1?

  1. Reply Christian López Espínola Apr 5,2007 10:34 pm

    Don’t forget: Javascript can be disabled, so validate the user input always on the server side! :)

  2. Reply a n n i e Apr 6,2007 12:53 am

    you’re so smart
    i don’t know all of this stuff >O

  3. Reply Michael Sync Apr 6,2007 3:03 am

    >>Don’t forget: Javascript can be disabled, so validate the user input always on the server side!

    ya.. good point.. thanks. man..

  4. Reply Ashish Apr 6,2007 12:40 pm

    added your link on my blog, add mine plz and also leave message if possible


  5. Reply Avenger069 Apr 12,2007 5:48 pm

    I wouldn’t say that “Obviously, this is a bug of ASP.NET”. Actually I believe this is being handled properly by ASP.NET.

    MultiLIne texboxes are rendered as a TEXTAREA to the client. There is no maxlength property on a TEXTAREA. See the spec here:

    The single line textboxes are rendered as an INPUT element which does have a maxlength property. See here:

    So what’s really happening when you change the TexMode property of the asp:textbox control is you are changing what gets rendered to the client. While I think the proper result is taking place I believe that MS should make it a little more obvious what is happening by maybe blanking out the maxlength property when multiline is selected.

  6. Reply Andrew Apr 17,2007 6:34 am

    Good, but how about Cut & Paste?

  7. Reply Michael Sync Apr 18,2007 3:11 am

    Hi Avenger069,

    Thanks for your comment and links. Avenger069
    Yeah. MS should do something about it. If they are not able to disable MaxLength property of ASP.NET TextBox then they should create the seperated controls for single textbox and textarea..

    Hi Andrew,
    >>how about Cut & Paste?
    Let me check about this.. I will reply you asap..

  8. Reply Amit May 22,2007 11:10 am


    instead of return false.
    event.returnValue = false;
    works better

  9. Reply Michael Sync May 22,2007 1:56 pm

    thanks. I’ll try to test it.
    Thanks again. Amit.

  10. Reply shobhit Jun 12,2007 5:32 am

    thans for the wonderfull article.
    but this function wont work when cut & paste..
    plz help…………

  11. Reply Michael Sync Jun 12,2007 6:59 am

    what errs are you getting??

  12. Reply Leo Jul 12,2007 4:29 pm

    I had the problem that i could no more delete text if max len was reached. Also i saw that this script works not if i paste some text longer then the max into this textfield..

    So i changed the function as follows:

    function checkMaxLen(txt,maxLen) {
    if(txt.value.length > (maxLen-1)) {
    var cont = txt.value;
    txt.value = cont.substring(0,(maxLen -1));
    return false;

    and i use the onkeyup instead of the onkeypressed – Event….

  13. Reply JDS Sep 13,2007 12:37 pm

    I want to point out another problem with the first solution. Once you actually reach the max length, the textarea won’t accept any more input. Trouble is this includes backspace, delete and arrow buttons.

  14. Reply Nisha Oct 9,2007 1:12 pm

    Thanks! it works. i stuck with this point. thanks again.

  15. Reply johny why May 9,2008 6:59 am

    i don’t quite agree with Avenger069 that it’s being handled properly by it may be correct to say that a textarea does not have a maxlength property, but as developer, i care not whether microsoft renders my asp:textbox as an input, a textarea, or a banana, as long as it behaves properly. that’s the whole point of– the programmer does not (ideally) need to be concerned with how the server controls are rendered– is supposed to generate browser-specific client-side script. maxlength is an essential and obvious property, which should be available for both single-line and multiline textboxes.

  16. Reply sathian Aug 20,2008 12:22 pm

    hi michael,

    I was searching for this script past 2 days in many site, now I got this script in your site, this is very helpful my project. That’s great. Thanks again.


    Sathian. K

  17. Reply Nimish Rao Jan 6,2009 9:52 am

    using the try catch block in JavaScript is useful?That is i can get the exception?thereby helping me in the impossible task of debugging JavaScript for ASP2.0?

  18. Reply almnynet Apr 18,2009 11:48 pm

    thanks man
    i shearch about it
    and your code working good :)

  19. Reply lamle Jun 19,2009 3:02 am

    Thanks for your information.

  20. Reply sgl Jul 22,2009 7:34 am

    good job mate. perfect.

  21. Reply Kurt Oct 26,2009 11:41 am

    Michael, very cool. I couldn’t use the regex solutions that are often suggested with the RegEx Validator Controls due to some strange postback business on my page. Leo makes the solution complete by adding cut&paste capability. I would suggest melding the two to one solution. One caveat: I don’t get taking maxlength-1. If the max length is 10 characters, then I want 10 characters max not 9.

  22. Reply Gregor suttie Mar 30,2010 5:19 am

    Unfortunate that I can paste in as much text as I want – which makes the necessity for server side validation very real.

  23. Reply gnew Oct 3,2010 11:38 pm

    thanks, works great :)

  24. Reply Gabo Nov 12,2010 9:36 am

    this works for me, only i quit out the sentence and i get it working in ASP.NET 3.5

    this my example…

  25. Reply suresh Gollapudi Oct 25,2011 9:29 pm

    this is very nice article!! .Ii am facing problem in my textbox Previasly if i reach maxlenth of textbox text char i am not getting for removing and delete but after added you code i am getting properly!!!!

    Thanks Alot!!!!!!!!!!!

  26. Reply kenshiroh Dec 26,2011 12:16 am

    Great article!

    As for the “Paste” problem, see article from the link below

    Now no need for server side validation

  27. Reply hazim rihani Jan 29,2012 7:34 am

    this will not work if you copied and pasted over 12 letters, 12 -1 = 11 !!

  28. Reply Dnyaneshwar Oct 31,2012 9:28 pm

    Then how o handle for dragDrop and copy pest..

Leave a Reply