TextBox's MaxLength does not work when TextMode=MultiLine

Discussion in 'ASP .Net Web Controls' 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,

    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
    #2
    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:
    18,975
    cnicol
    Sep 26, 2007
  2. RSB
    Replies:
    3
    Views:
    2,308
    i-Safire
    May 25, 2004
  3. Craig G
    Replies:
    2
    Views:
    925
    Karl Seguin
    Feb 15, 2005
  4. Nathan Sokalski
    Replies:
    3
    Views:
    64,854
    zed_rhodes
    May 29, 2009
  5. danthman
    Replies:
    11
    Views:
    10,102
    elizas
    May 11, 2010
Loading...

Share This Page