Textarea

Discussion in 'Javascript' started by lamp, Apr 26, 2007.

  1. lamp

    lamp Guest

    Hello,

    I want 1 Textarea which can take only 20 characters. It can't input
    more
    than 20 characters.

    Please, guide me.

    Thanks in advance
     
    lamp, Apr 26, 2007
    #1
    1. Advertising

  2. lamp

    Tom Cole Guest

    On Apr 26, 6:32 am, lamp <> wrote:
    > Hello,
    >
    > I want 1 Textarea which can take only 20 characters. It can't input
    > more
    > than 20 characters.
    >
    > Please, guide me.
    >
    > Thanks in advance


    You can use the onkeyup event to capture each keypress and test for
    length. If over 20 then truncate it to the first 20. Use onkeyup,
    because onkeypress or onkeydown will append the character AFTER your
    function has been called, resulting in 21 characters, where the 21st
    character changes to the last typed character.

    For example let's say we have a textarea with id="test":

    function testLength() {
    var value = document.getElementById('test').value;
    if (value.length > 20) {
    document.getElementById('test').value = value.substring(0, 20);
    }
    }

    <textarea id="test" onkeyup="testLength();"></textarea>

    Now the test textarea will be limited to 20 characters. HTH.
     
    Tom Cole, Apr 26, 2007
    #2
    1. Advertising

  3. lamp

    RobG Guest

    lamp wrote:
    > Hello,
    >
    > I want 1 Textarea which can take only 20 characters. It can't input
    > more
    > than 20 characters.


    Use an input type text, set the maxlength attribute to 20. No script
    required.


    --
    Rob
     
    RobG, Apr 26, 2007
    #3
  4. lamp

    Evertjan. Guest

    Tom Cole wrote on 26 apr 2007 in comp.lang.javascript:

    > On Apr 26, 6:32 am, lamp <> wrote:
    >> Hello,
    >>
    >> I want 1 Textarea which can take only 20 characters. It can't input
    >> more
    >> than 20 characters.
    >>
    >> Please, guide me.
    >>
    >> Thanks in advance

    >
    > You can use the onkeyup event to capture each keypress and test for
    > length. If over 20 then truncate it to the first 20. Use onkeyup,
    > because onkeypress or onkeydown will append the character AFTER your
    > function has been called, resulting in 21 characters, where the 21st
    > character changes to the last typed character.
    >
    > For example let's say we have a textarea with id="test":
    >
    > function testLength() {
    > var value = document.getElementById('test').value;
    > if (value.length > 20) {
    > document.getElementById('test').value = value.substring(0,
    > 20);
    > }
    >}


    No "if" needed!

    ===========================================
    <script type='text/javascript'>
    function testLength(v) {
    v.value = v.value.substr(0,20);
    }
    </script>

    <textarea onkeyup='testLength(this);'></textarea>
    ===========================================

    >
    > <textarea id="test" onkeyup="testLength();"></textarea>
    >
    > Now the test textarea will be limited to 20 characters. HTH.


    But how would you know the letter typed in was the rightmost one?

    Try this:

    ==========================
    <script type='text/javascript'>
    var textboxtextSave = '';

    function testLength(test) {
    if (test.value.length > 20)
    test.value = textboxtextSave
    else
    textboxtextSave = test.value;
    };
    </script>

    <textarea onkeyup='testLength(this);'></textarea>
    ==========================

    [This also will warrant some ctrl-V conciderations.]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Apr 26, 2007
    #4
  5. lamp

    shimmyshack Guest

    On Apr 26, 11:32 am, lamp <> wrote:
    > Hello,
    >
    > I want 1 Textarea which can take only 20 characters. It can't input
    > more
    > than 20 characters.
    >
    > Please, guide me.
    >
    > Thanks in advance


    dont forget that any script you use cannot stop POST data alteration
    before it arrives on your server, so is no guarantee that the maximum
    will be honoured; as well as this, you might find that using the
    onchange attribute is better than onkeyup say, because people might
    simply copy and paste using the mouse.
     
    shimmyshack, Apr 26, 2007
    #5
  6. lamp

    shimmyshack Guest

    On Apr 26, 11:32 am, lamp <> wrote:
    > Hello,
    >
    > I want 1 Textarea which can take only 20 characters. It can't input
    > more
    > than 20 characters.
    >
    > Please, guide me.
    >
    > Thanks in advance


    well i shouldnt have said onchange of course! and im still not sure
    how to trap the mouse paste event without using a messy interval
    check, it wont be "live"

    you could also provide user feedback using a disabled input, say:
    <textarea name="textareaname" onkeyup="return checknumber()"
    onfocus="checknumber()" onblur="checknumber()"></textarea>
    <input disabled type="text" name="remaining" size="3" value=""/>

    and the following function is called each time a user types somthing:

    function checknumber()
    {

    var msgLen = 20;
    var maxChars = msgLen;
    var numChars = document.formname.textareaname.value.length;

    if (numChars > maxChars)
    {
    document.formname.textareaname.value =
    document.formname.textareaname.value.substring(0,maxChars);
    numChars = maxChars;
    document.formname.textareaname.focus();
    r = false;
    }
    else {
    r = true;
    }

    var c = numChars;
    var numMsgs = 0;
    while (c > 0)
    {
    numMsgs++;
    c -= msgLen;
    }

    document.formname.remaining.value = maxChars - numChars;
    return r;
    }

    you might have to print the textarea and input dynamically if the
    input doesnt update as required.
     
    shimmyshack, Apr 26, 2007
    #6
    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. Bei

    msword-like textarea?

    Bei, Jun 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,267
    mike C.
    Jul 2, 2003
  2. Replies:
    3
    Views:
    798
    Justin Lovell
    Oct 5, 2003
  3. Augustus
    Replies:
    1
    Views:
    397
    Ken Schaefer
    Sep 10, 2003
  4. Textarea Inside of a textarea

    , Feb 4, 2006, in forum: ASP General
    Replies:
    6
    Views:
    362
    Anthony Jones
    Feb 5, 2006
  5. Replies:
    1
    Views:
    357
    Bart Van der Donck
    Jul 4, 2007
Loading...

Share This Page