locked
Maxlength with Multiline textbox? RRS feed

  • Question

  • User-1210839387 posted

    I have an asp.net/vb.net 2005 page,

    on this page I have a textbox with Textmode="Multiline".

    I am trying to set MaxLength on the textbox but maxlength is not working.  IE: if i set maxlength to 10 it does not limit the length i can keep typing well over 10 characters.

    Does MaxLength property work right if you have multiline textbox?  If not does anyone know a way around this?

    Wednesday, December 12, 2007 8:01 AM

Answers

All replies

  • User-1995538749 posted

    Yes, the MaxLength property has no affect when in MultiLine mode. See the following thread:

    http://forums.asp.net/p/40065/172135.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 12, 2007 8:05 AM
  • User-1411267907 posted

    Well there is no straight solution for this you either use regex or javascript.

    http://www.velocityreviews.com/forums/t77471-maxlength-on-multiline-textbox.html

    may be this might help you.

    Wednesday, December 12, 2007 8:08 AM
  • User-741714743 posted

    Single line textboxes render as an HTML <input> tag - which supports a max
    length.

    The Multiline Textbox, renders as an HTML <TextArea> which doesn't support a
    length property.

    So, one workaround is to roll your own by calling a JavaScript function that
    checks the length of the text entered. The function gets called on every
    keypress and denies further data entry when the max length is reached. Of
    course the JavaScript function needs to ignore some keys. This one should
    work. It limits to 300 characters:

    This is how your multiline text box will render.
    <textarea name="MyTextArea" rows="3" id="MyTextArea" onkeypress="return
    checkMaxLength(event,this)" TAMaxLength="300"
    style="width:350px;"></textarea>

    Here's your JavaScript function:
    function checkMaxLength(e,el) {
    switch(e.keyCode) {
    case 37: // left
    return true;
    case 38: // up
    return true;
    case 39: // right
    return true;
    case 40: // down
    return true;
    case 8: // backspace
    return true;
    case 46: // delete
    return true;
    case 27: // escape
    el.value='';
    return true;
    }
    return (el.value.length<el.getAttribute("TAMaxLength"));
    }

    Take from here http://www.velocityreviews.com/forums/t109250-textboxs-maxlength-does-not-work-when-textmodemultiline.html

    For Extending TextBox, see here http://www.codeproject.com/KB/aspnet/Textarea_Length_Validator.aspx?print=true

    Wednesday, December 12, 2007 8:10 AM
  • User-1210839387 posted

    thanks,

    i forgot to mention that the textbox is in an .ascx page, is there a way to put the javascript in that page or do i have to put the javascript in a separate file?

    Wednesday, December 12, 2007 8:13 AM
  • User-1995538749 posted

    Sure, you can put a script tag directly on the page if you'd like.

    Wednesday, December 12, 2007 8:35 AM
  • User-1210839387 posted

    thanks. a question: can i keep the control a textbox and still implement the javascript event to solve this?

    do i have to change the textbox to be a textarea?

     

    I tried doing this and using ontextchanged event but this is not working.

    Wednesday, December 12, 2007 9:21 AM
  • User-1995538749 posted

    When you set the TextMode property to "MultiLine", the control is automatically rendered as a TextArea as opposed to an Input control. If you don't want to use JavaScript, then simply use a RegularExpressionValidator.

    Wednesday, December 12, 2007 9:41 AM
  • User-197225127 posted

    Here is the simplest solution to this known prblem..

    http://dotnetfocus.blogspot.com/2009/08/systemwebuiwebcontrolstextbox-maxlength.html 

    Hope it will help...

    Monday, August 3, 2009 8:32 AM