How to restrict the number of lines in a text area ?

Discussion in 'HTML' started by Anz, Jul 1, 2008.

  1. Anz

    Anz Guest

    Is it possible to limit the number of lines in a text area ?,
    I have a text area, in which i have to limit the number of lines up to
    3,
    How can i done this?, I wrote the tag as

    <textarea name="short_desc" id="short_desc" cols="25" rows="3" ></
    textarea>

    But this doesn't help me, pls reply
    Anz, Jul 1, 2008
    #1
    1. Advertising

  2. Anz

    BootNic Guest

    On Mon, 30 Jun 2008 22:05:26 -0700 (PDT)
    Anz <> wrote in:
    <>

    > Is it possible to limit the number of lines in a text area ?, I have
    > a text area, in which i have to limit the number of lines up to 3,
    > How can i done this?, I wrote the tag as
    >
    > <textarea name="short_desc" id="short_desc" cols="25" rows="3" ></
    > textarea>
    >
    > But this doesn't help me, pls reply


    Are you sure you just want to limit the number of rows in a textarea?
    Perhaps a silly question.

    However if you decide you wish to limit the number of charters in the
    textarea, with the aid of javascript, you may find what you need from
    google.

    http://tinyurl.com/3n44us

    Keep in mind it will still need to be checked on the server.




    --

    BootNic Tue Jul 1, 2008 02:01 am
    If the facts don't fit the theory, change the facts.
    *Albert Einstein*

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)

    iEYEARECAAYFAkhpyE4ACgkQylMUzZO6jeJlvQCggCn/nL7uLAu8+ShKFmlfrUuD
    HggAn1BPNNczXZTVEXJP5zQKtOzhm/1F
    =OKrO
    -----END PGP SIGNATURE-----
    BootNic, Jul 1, 2008
    #2
    1. Advertising

  3. Anz

    Anz Guest

    Thanks, i just want to limit the number of lines in the text
    area, ...not the chars
    Anz, Jul 1, 2008
    #3
  4. Anz

    BootNic Guest

    On Mon, 30 Jun 2008 23:11:23 -0700 (PDT)
    Anz <> wrote in:
    <>

    >
    > Thanks, i just want to limit the number of lines in the text area,
    > ...not the chars


    Interesting. Well all is not lost just yet. You can still have a look
    at some of them examples, pick on you kind of like.

    Then after that all one has to do is count line returns in place of the
    charters.

    Go make yourself an example page to share with everyone in the
    newsgroup, try to modify one of the scripts to do what you need it to.

    Then when you need help with it, try posting in comp.lang.javascript.

    --

    BootNic Tue Jul 1, 2008 02:21 am
    Have no fear of perfection - you'll never reach it.
    *Salvador Dali*

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)

    iEYEARECAAYFAkhpzPYACgkQylMUzZO6jeIZdgCfelImD3EAgCJIcEIiEU/suEAP
    Zm0An2gfUCaLWvMoK+B6U2U6gWidp77t
    =jVrN
    -----END PGP SIGNATURE-----
    BootNic, Jul 1, 2008
    #4
  5. Gazing into my crystal ball I observed Anz <> writing
    in news::

    >
    > Thanks, i just want to limit the number of lines in the text
    > area, ...not the chars


    Please understand there really is no way to count LINES, only characters.
    Why? Because the width of a character can change with the UI. Ever had a
    word document that was one page and a second page with a little bit of
    text? To fit it all on one page, Word first reduces the font size. So, you
    see, you really cannot use lines.

    Additionally, most people are used to see _characters_. What you _can_ do
    is state something like:
    Maxium characters is 200, about 4 lines. Then include javascript that
    counts the characters and presents how many are left. Of course, you will
    have to check the amount of characters server side.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
    Adrienne Boswell, Jul 1, 2008
    #5
  6. Anz wrote:
    > Thanks, i just want to limit the number of lines in the text
    > area, ...not the chars


    Cannot be done with HTML. Must count new line characters, first with
    JavaScript then finally with server-side...

    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO
    http://www.LittleWorksStudio.com
    Jonathan N. Little, Jul 1, 2008
    #6
  7. Anz

    Guest

    On Jul 1, 6:05 am, Anz <> wrote:
    > Is it possible to limit the number of lines in a text area ?,
    > I have a text area, in which i have to limit the number of lines up to
    > 3,
    > How can i done this?, I wrote the tag as
    >
    > <textarea name="short_desc"  id="short_desc" cols="25" rows="3" ></
    > textarea>
    >
    > But this doesn't help me, pls reply


    Hi, i've had to solve this problem today. Its true that you can't
    solve this with HTML. But the javascript to achieve it is quite
    simple, my solution is as follows:

    This is for a textarea that is being used as an address entry box, i'm
    limiting it here to four lines but you can change it to whatever size
    you need

    1) Attach some javascript to the onkeyup event of the textarea
    2) Split the textarea string into an array using split() on the \n
    char
    3) Count the number of lines in the textarea by getting the number of
    elements in the newly created string array
    4) Setting the value of the text area equal to lines 1,2,3
    5) Making the string in the array for line 4 equal to a substring of
    itself that is missing the last character (else you end up with 4
    lines and a newline)
    6) Output a notice to the user that the input field has too many
    lines.

    The code looks like this:
    onkeyup="var newlineCount = this.value.split('\n');
    if(newlineCount.length>4) { this.value =
    newlineCount[0]+newlineCount[1]+newlineCount[2]+
    (newlineCount[3].substr(0,newlineCount[3].length-1)); alert('Maximum
    Address Size: Four Lines');}"

    Hope this is useful,

    Regards,
    John.
    , Jul 2, 2008
    #7
  8. Scripsit Jonathan N. Little:

    > Anz wrote:
    >> Thanks, i just want to limit the number of lines in the text
    >> area, ...not the chars

    >
    > Cannot be done with HTML. Must count new line characters, first with
    > JavaScript then finally with server-side...


    So, as so often, the _real_ question is what the OP is doing. What is
    the pragmatic goal, and how can you illustrate with a URL?

    Moreover, _if_ restrictions are really needed, they should _first_ be
    implemented server-side, because there they can work reliably.

    Then you might consider using three single-line input fields, possibly
    with maxlength attributes. This will _not_ ensure that data is within
    the limitations, since anyone and his brother can create a modified
    version of the form and submit any data he wants. But it may help normal
    users to understand the limitations and live with them.

    --
    Jukka K. Korpela ("Yucca")
    http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jul 3, 2008
    #8
  9. Anz

    Anz Guest

    On Jul 2, 6:59 pm, wrote:
    > On Jul 1, 6:05 am, Anz <> wrote:
    >
    > > Is it possible to limit the number of lines in a text area ?,
    > > I have a text area, in which i have to limit the number of lines up to
    > > 3,
    > > How can i done this?, I wrote the tag as

    >
    > > <textarea name="short_desc" id="short_desc" cols="25" rows="3" ></
    > > textarea>

    >
    > > But this doesn't help me, pls reply

    >
    > Hi, i've had to solve this problem today. Its true that you can't
    > solve this with HTML. But the javascript to achieve it is quite
    > simple, my solution is as follows:
    >
    > This is for a textarea that is being used as an address entry box, i'm
    > limiting it here to four lines but you can change it to whatever size
    > you need
    >
    > 1) Attach some javascript to the onkeyup event of the textarea
    > 2) Split the textarea string into an array using split() on the \n
    > char
    > 3) Count the number of lines in the textarea by getting the number of
    > elements in the newly created string array
    > 4) Setting the value of the text area equal to lines 1,2,3
    > 5) Making the string in the array for line 4 equal to a substring of
    > itself that is missing the last character (else you end up with 4
    > lines and a newline)
    > 6) Output a notice to the user that the input field has too many
    > lines.
    >
    > The code looks like this:
    > onkeyup="var newlineCount = this.value.split('\n');
    > if(newlineCount.length>4) { this.value =
    > newlineCount[0]+newlineCount[1]+newlineCount[2]+
    > (newlineCount[3].substr(0,newlineCount[3].length-1)); alert('Maximum
    > Address Size: Four Lines');}"
    >
    > Hope this is useful,
    >
    > Regards,
    > John.


    Thank u Mr. John
    Anz, Jul 12, 2008
    #9
    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. Mark
    Replies:
    1
    Views:
    572
    Larry Barowski
    Jun 27, 2005
  2. hotadvice
    Replies:
    14
    Views:
    691
    hotadvice
    Oct 2, 2007
  3. leo
    Replies:
    1
    Views:
    260
    Bob Lehmann
    Dec 5, 2005
  4. Matthew Huggett
    Replies:
    2
    Views:
    199
    Hidetoshi NAGAI
    Feb 15, 2004
  5. Replies:
    2
    Views:
    104
Loading...

Share This Page