Maximum characters in TextArea

Discussion in 'HTML' started by Mr. x, Nov 29, 2003.

  1. Mr. x

    Mr. x Guest

    Hello,
    How can I limit the maximum characters that can be entered in the <textArea>
    (using html attributes) ?

    Thanks :)
     
    Mr. x, Nov 29, 2003
    #1
    1. Advertising

  2. Mr. x

    Nico Schuyt Guest

    Mr. x wrote:
    > How can I limit the maximum characters that can be entered in the
    > <textArea> (using html attributes) ?


    By reading a small booklet ($5) "HTML for starters"
    You should have a least a basic knowledge before asking *everything* in a
    newsgroup.
    Nico
     
    Nico Schuyt, Nov 29, 2003
    #2
    1. Advertising

  3. Mr. x

    Chris Morris Guest

    "Mr. x" <> writes:
    > How can I limit the maximum characters that can be entered in the <textArea>
    > (using html attributes) ?


    Why do you need to? (as always, an example URL would be great, if
    possible) If it's so a server-side process doesn't get given input too
    long, you still need to validate server-side that it works if they go
    over the limit, to defend yourself against other people writing
    similar looking forms without that limitation and/or using browsers
    that don't support that attribute.

    You could politely ask them to keep their content concise, I suppose.

    --
    Chris
     
    Chris Morris, Nov 29, 2003
    #3
  4. Mr. x

    Steve Pugh Guest

    "Mr. x" <> wrote:

    >How can I limit the maximum characters that can be entered in the <textArea>
    >(using html attributes) ?


    You can't using HTML attributes.

    You have to check the length of the submission in the server side
    script that accepts you form submission, and optionally in a client
    side script (e.g. JavaScript) as well.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <> <http://steve.pugh.net/>
     
    Steve Pugh, Nov 29, 2003
    #4
  5. Mr. x

    Mr. x Guest

    > You can't using HTML attributes.
    I mean attributes of <textarea> element, that can limit the maximum size of
    characters.

    > You have to check the length of the submission in the server side
    > script that accepts you form submission, and optionally in a client
    > side script (e.g. JavaScript) as well.

    Is this the only way to do so (what if user enters milion of character - I
    still need to check milion of characters.

    Thanks :)
     
    Mr. x, Nov 29, 2003
    #5
  6. "Mr. x" <> wrote:

    >> You can't using HTML attributes.

    > I mean attributes of <textarea> element, that can limit the maximum
    > size of characters.


    They are HTML attributes. The phrase "HTML attribute" stands for the
    attribute of any element in HTML markup. The answer stands; there is no
    attribute, in <textarea> or elsewhere, for limiting the size.

    >> You have to check the length of the submission in the server side
    >> script that accepts you form submission, and optionally in a
    >> client side script (e.g. JavaScript) as well.

    > Is this the only way to do so (what if user enters milion of
    > character - I still need to check milion of characters.


    It is the only way. How you do that is a different thing. Your form
    handler could first just look at the Content-Length header in HTTP
    headers and discard the submission (politely, perhaps) if there is no
    such header or its value is larger than some limit. You would still
    need the code for actually processing the data when its size is
    acceptable, and check the amount of data - since a cracker could have
    faked Content-Length: 42 and still send you megabytes of junk.
    This shouldn't be a problem, since the handler needs to process every
    character anyway, and counting them (one way or another) is just a
    minor side job to be done.

    More info: http://www.cs.tut.fi/~jkorpela/forms/textarea.html

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Nov 29, 2003
    #6
  7. Mr. x

    Steve Pugh Guest

    "Mr. x" <> wrote:

    >> You can't using HTML attributes.

    >I mean attributes of <textarea> element, that can limit the maximum size of
    >characters.


    I know what you meant and my answer applies.

    You can not set a maximum length for the content of the textarea
    element with any attribute in HTML.

    >> You have to check the length of the submission in the server side
    >> script that accepts you form submission, and optionally in a client
    >> side script (e.g. JavaScript) as well.

    >Is this the only way to do so (what if user enters milion of character - I
    >still need to check milion of characters.


    Yes.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <> <http://steve.pugh.net/>
     
    Steve Pugh, Nov 29, 2003
    #7
  8. Mr. x

    Chris Morris Guest

    "Mr. x" <> writes:
    > > You can't using HTML attributes.

    > I mean attributes of <textarea> element, that can limit the maximum size of
    > characters.


    There aren't any.

    > > You have to check the length of the submission in the server side
    > > script that accepts you form submission, and optionally in a client
    > > side script (e.g. JavaScript) as well.

    > Is this the only way to do so (what if user enters milion of character - I
    > still need to check milion of characters.


    In PHP it's trivial to do:

    Say your maximum length is 300 chars and your textarea is called
    'input', and the form is POSTed.

    <?php
    if (strlen($_POST['input']) > 300) {
    // appropriate rejecting mechanism
    } else {
    // do what you want with it.
    }
    ?>

    Similar is possible (probably as easily) in other server-side languages.

    And yes, it's the only safe way to do it. Otherwise there's nothing
    to stop someone writing their own form and using that instead.

    --
    Chris
     
    Chris Morris, Nov 29, 2003
    #8
  9. Mr. x

    Richard Guest

    Chris! wrote:

    > "Mr. x" <> writes:
    >> How can I limit the maximum characters that can be entered in the
    >> <textArea> (using html attributes) ?


    > Why do you need to? (as always, an example URL would be great, if
    > possible) If it's so a server-side process doesn't get given input too
    > long, you still need to validate server-side that it works if they go
    > over the limit, to defend yourself against other people writing
    > similar looking forms without that limitation and/or using browsers
    > that don't support that attribute.


    > You could politely ask them to keep their content concise, I suppose.


    > --
    > Chris



    www.abuse.charter.net
    Limits text to 2,000 characters.
     
    Richard, Nov 29, 2003
    #9
  10. Mr. x

    Mr. x Guest


    > www.abuse.charter.net
    > Limits text to 2,000 characters.


    The page cannot be displayed - wrong link ?!

    Thanks :)
     
    Mr. x, Nov 29, 2003
    #10
  11. Mr. x

    Louis Somers Guest

    "Mr. x" <> wrote in news:3fc88c51$1@news.012.net.il:

    > Hello,
    > How can I limit the maximum characters that can be entered in the
    > <textArea> (using html attributes) ?
    >
    > Thanks :)


    With a single line ( input type="text" ) you can use maxlength="10" as
    attribute. Not with a textarea.

    With a textarea you will need some javascript. Here is an example:

    ---- javascript example ----

    <html><head>

    <script type="text/javascript" language="JavaScript">
    function calcCharLeft(theForm)
    {
    var mytext = theForm.thetext.value;
    var availChars = 140;
    if (mytext.length > availChars) {
    theForm.thetext.value = mytext.substring(0,availChars);
    theForm.fChars.value = 0;
    alert('Your text is too long');
    theForm.thetext.focus();
    }
    else {
    theForm.fChars.value = availChars - mytext.length;
    }
    }

    </script>
    <title>limit chars</title>
    </head>
    <body>
    <form method="post" action="yoururl" name="myForm">
    <textarea name="thetext" rows="7" cols="26" onkeypress="calcCharLeft
    (this.form)" onkeyup="calcCharLeft(this.form)" onkeydown="calcCharLeft
    (this.form)" onchange="calcCharLeft(this.form)"></textarea>
    <input class="disabled" align="right" type="text" name="fChars"
    size="4" value="140" disabled onfocus="this.form.msg.focus()">
    </form>
    </body></html>

    ---- end javascript example ----

    Cheers,
    Louis

    --
    News Updater, No scripts, No Database
    http://www.xmlssoftware.com/NUpdater
     
    Louis Somers, Nov 29, 2003
    #11
  12. Mr. x

    Steve Pugh Guest

    "Richard" <anonymous@127.000> wrote:

    >www.abuse.charter.net


    404

    Presumably you meant http://abuse.charter.net/

    >Limits text to 2,000 characters.


    No it doesn't. I've just posted over 15,000 characters. I had
    JavaScript enabled. No error message came back from the server.

    They use:
    <textarea [snip] maxlength=2000></textarea> which is not valid and is
    not supported by any browser as far as I'm aware. It may be supported
    by some browsers but relying on it would be highly foolish.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <> <http://steve.pugh.net/>
     
    Steve Pugh, Nov 30, 2003
    #12
  13. Mr. x

    Richard Guest

    Mr.! wrote:


    >> www.abuse.charter.net
    >> Limits text to 2,000 characters.


    > The page cannot be displayed - wrong link ?!


    > Thanks :)


    http://abuse.charter.net

    whenever that happens, remove the www and try again. Many of the newer
    domains work only one way, not both.
    "Cannot be displayed" is not the same as "not found".
     
    Richard, Nov 30, 2003
    #13
  14. Mr. x

    Richard Guest

    Steve! wrote:

    > "Richard" <anonymous@127.000> wrote:


    >>www.abuse.charter.net


    > 404


    > Presumably you meant http://abuse.charter.net/


    >>Limits text to 2,000 characters.


    > No it doesn't. I've just posted over 15,000 characters. I had
    > JavaScript enabled. No error message came back from the server.


    > They use:
    > <textarea [snip] maxlength=2000></textarea> which is not valid and is
    > not supported by any browser as far as I'm aware. It may be supported
    > by some browsers but relying on it would be highly foolish.


    > Steve


    Well you know those highly trained tech types right?
    But I did post an overlength message one time and did get the error message.
    So perhaps it only works in IE.
     
    Richard, Nov 30, 2003
    #14
  15. Mr. x

    Richard Guest

    Steve! wrote:

    > "Mr. x" <> wrote:


    >>> You can't using HTML attributes.

    >>I mean attributes of <textarea> element, that can limit the maximum size
    >>of characters.


    > I know what you meant and my answer applies.


    I am God, you will do it my way or I'll zap you with a lightning bolt.
    Capiche?


    > You can not set a maximum length for the content of the textarea
    > element with any attribute in HTML.


    >>> You have to check the length of the submission in the server side
    >>> script that accepts you form submission, and optionally in a client
    >>> side script (e.g. JavaScript) as well.

    >>Is this the only way to do so (what if user enters milion of character -
    >>I still need to check milion of characters.


    > Yes.


    > Steve


    > --
    > "My theories appal you, my heresies outrage you,
    > I never answer letters and you don't like my tie." - The Doctor


    > Steve Pugh <> <http://steve.pugh.net/>
     
    Richard, Nov 30, 2003
    #15
  16. Mr. x

    Steve Pugh Guest

    "Richard" <anonymous@127.000> wrote:
    > Steve! wrote:
    > > "Richard" <anonymous@127.000> wrote:

    >
    > > Presumably you meant http://abuse.charter.net/

    >
    > >>Limits text to 2,000 characters.

    >
    > > No it doesn't. I've just posted over 15,000 characters. I had
    > > JavaScript enabled. No error message came back from the server.

    >
    > > They use:
    > > <textarea [snip] maxlength=2000></textarea> which is not valid and is
    > > not supported by any browser as far as I'm aware. It may be supported
    > > by some browsers but relying on it would be highly foolish.

    >
    >Well you know those highly trained tech types right?


    I know some of them. Not the ones responsible for the page in
    question.

    >But I did post an overlength message one time and did get the error message.


    An error message would result from client or server side scripting.
    This HTML attribute, if it did anything at all, would presumably work
    in the same way as the same attribute on input and simply not allow
    any extra characters to be typed into the box, i.e. no error message.

    >So perhaps it only works in IE.


    Not IE6, Win XP. Just posted 3000 characters, no error message.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <> <http://steve.pugh.net/>
     
    Steve Pugh, Nov 30, 2003
    #16
  17. Mr. x

    Steve Pugh Guest

    "Richard" <anonymous@127.000> wrote:
    > Steve! wrote:
    > > "Mr. x" <> wrote:

    >
    > >>> You can't using HTML attributes.
    > >>I mean attributes of <textarea> element, that can limit the maximum size
    > >>of characters.

    >
    > > I know what you meant and my answer applies.

    >
    >I am God, you will do it my way or I'll zap you with a lightning bolt.
    >Capiche?


    I don't know, what is your way? So far all you've posted to this
    thread is the incorrect URL of a page that uses a non-existent
    attribute that doesn't do what the OP wants. So what is your way,
    after all I wouldn't want to get hit by a lightning bolt for not doing
    it.

    Steve

    --
    "My theories appal you, my heresies outrage you,
    I never answer letters and you don't like my tie." - The Doctor

    Steve Pugh <> <http://steve.pugh.net/>
     
    Steve Pugh, Nov 30, 2003
    #17
  18. Mr. x

    Sam Hughes Guest

    "Nico Schuyt" <> wrote in news:3fc88e02$0$164
    $:

    > Mr. x wrote:
    >> How can I limit the maximum characters that can be entered in the
    >> <textArea> (using html attributes) ?

    >
    > By reading a small booklet ($5) "HTML for starters"
    > You should have a least a basic knowledge before asking *everything* in a
    > newsgroup.
    > Nico


    When brucie's Lack-of-Life Quotient first came about, nobody expected a
    person to get a high LOLQ by asking questions and not knowing anything.
    But I think we might have found a counterexample.
     
    Sam Hughes, Nov 30, 2003
    #18
  19. Mr. x

    FreeFaller

    Joined:
    Oct 2, 2009
    Messages:
    1
    Javascript makes it easy

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    function limitText(element,limit) {
    if (element.value.length > limit) {
    element.value = element.value.substring(0, limit);
    }
    }
    </script>

    </head>

    <body>

    <form>
    <textarea onKeyUp="limitText(this,20);" onKeyDown="limitText(this,20);" name="question" cols="50" rows="5" ></textarea>
    </form>
    </body>
    </html>
     
    FreeFaller, Oct 2, 2009
    #19
    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. John
    Replies:
    1
    Views:
    5,693
    Owen Jacobson
    Jul 31, 2007
  2. Augustus
    Replies:
    1
    Views:
    357
    Ken Schaefer
    Sep 10, 2003
  3. RICHARD BROMBERG

    Textarea maximum exceeded

    RICHARD BROMBERG, Apr 12, 2005, in forum: ASP General
    Replies:
    4
    Views:
    248
    Dave Anderson
    Apr 13, 2005
  4. Textarea Inside of a textarea

    , Feb 4, 2006, in forum: ASP General
    Replies:
    6
    Views:
    335
    Anthony Jones
    Feb 5, 2006
  5. phanhuyich
    Replies:
    4
    Views:
    276
Loading...

Share This Page