TextBox's MaxLength does not work when TextMode=MultiLine

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 4, 2005.

  1. When using a MultiLine TextBox (which generates a TextArea) setting the
    MaxLength property has no effect. However, because I am saving the contents
    of the TextBox to a DB field, I must limit the number characters allowed. I
    would prefer not to do this serverside because that would require a large
    amount of trips between the server and client (especially for a 200
    character limit). Is there a way to do this? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Sep 4, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Jordan Guest

    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"));
    }

    -HTH

    "Nathan Sokalski" <> wrote in message
    news:...
    > When using a MultiLine TextBox (which generates a TextArea) setting the
    > MaxLength property has no effect. However, because I am saving the
    > contents of the TextBox to a DB field, I must limit the number characters
    > allowed. I would prefer not to do this serverside because that would
    > require a large amount of trips between the server and client (especially
    > for a 200 character limit). Is there a way to do this? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Jordan, Sep 4, 2005
    #2
    1. Advertising

  3. Nathan,

    While Jordan's solution is certainly a viable one I prefer to use a regular
    expression validator with the following regular expression in it:
    ^[\s\S]{0,25}$

    Change the "25" to whatever the max character length allowed is.

    This type of validator validates client side if possible, but also validates
    server side so that no-one can bypass the check.

    It also works even if someone has turned off javascript.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Jordan" <> wrote in message
    news:...
    > 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"));
    > }
    >
    > -HTH
    >
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >> When using a MultiLine TextBox (which generates a TextArea) setting the
    >> MaxLength property has no effect. However, because I am saving the
    >> contents of the TextBox to a DB field, I must limit the number characters
    >> allowed. I would prefer not to do this serverside because that would
    >> require a large amount of trips between the server and client (especially
    >> for a 200 character limit). Is there a way to do this? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    S. Justin Gengo, Sep 4, 2005
    #3
  4. Nathan Sokalski

    zed_rhodes

    Joined:
    May 29, 2009
    Messages:
    1
    I know this is an old thread but just wanted to point out something I discovered that may save others a bit of time.

    may not be the best option. The technique employed seems to be easily defeatable if you hold down a key and then, while still holding it down, click in another field. I was able to insert an arbitrary number of characters into the textarea "with a maxlength of 10" in this manner, which remained in the field after I set the focus to another field. It seems the user would have to be intentionally trying to break something - but with some users you never know.

    I can't speak for other browsers, but for IE7 I've had better luck with the regular expression method.
    zed_rhodes, May 29, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hardcoded

    maxlength on multiline textbox

    hardcoded, May 5, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    19,015
    cnicol
    Sep 26, 2007
  2. RSB
    Replies:
    3
    Views:
    2,323
    i-Safire
    May 25, 2004
  3. Craig G
    Replies:
    2
    Views:
    942
    Karl Seguin
    Feb 15, 2005
  4. danthman
    Replies:
    11
    Views:
    10,137
    elizas
    May 11, 2010
  5. Nathan Sokalski

    TextBox's MaxLength does not work when TextMode=MultiLine

    Nathan Sokalski, Sep 4, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    170
    S. Justin Gengo
    Sep 4, 2005
Loading...

Share This Page