Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Textarea

Reply
Thread Tools

Textarea

 
 
lamp
Guest
Posts: n/a
 
      04-26-2007
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

 
Reply With Quote
 
 
 
 
Tom Cole
Guest
Posts: n/a
 
      04-26-2007
On Apr 26, 6:32 am, lamp <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-26-2007
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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      04-26-2007
Tom Cole wrote on 26 apr 2007 in comp.lang.javascript:

> On Apr 26, 6:32 am, lamp <(E-Mail Removed)> 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)
 
Reply With Quote
 
shimmyshack
Guest
Posts: n/a
 
      04-26-2007
On Apr 26, 11:32 am, lamp <(E-Mail Removed)> 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.

 
Reply With Quote
 
shimmyshack
Guest
Posts: n/a
 
      04-26-2007
On Apr 26, 11:32 am, lamp <(E-Mail Removed)> 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,m axChars);
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add </textarea> within <textarea> tags? frank.moens@gmail.com Javascript 1 07-04-2007 04:00 PM
Textarea Inside of a textarea wperry1@gmail.com ASP General 6 02-05-2006 08:00 AM
remembering last position in textbox / textarea and returning to after post / reload jason@cyberpine.com ASP .Net 3 10-05-2003 05:56 PM
Removing carriage returns from <textarea></textarea> input Augustus ASP General 1 09-10-2003 04:55 AM
msword-like textarea? Bei ASP .Net 2 07-02-2003 04:46 PM



Advertisments