469,272 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

Textarea

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

Apr 26 '07 #1
6 2271
I want 1 Textarea which can take only 20 characters. It can't input
more
than 20 characters.
Try this :
http://www.rgagnon.com/jsdetails/js-0091.html

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
Apr 26 '07 #2
On Apr 26, 6:32 am, lamp <lam...@gmail.comwrote:
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.

Apr 26 '07 #3
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
Apr 26 '07 #4
Tom Cole wrote on 26 apr 2007 in comp.lang.javascript:
On Apr 26, 6:32 am, lamp <lam...@gmail.comwrote:
>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)
Apr 26 '07 #5
On Apr 26, 11:32 am, lamp <lam...@gmail.comwrote:
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.

Apr 26 '07 #6
On Apr 26, 11:32 am, lamp <lam...@gmail.comwrote:
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.

Apr 26 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Csaba Gabor | last post: by
5 posts views Thread by Jesper Rønn-Jensen | last post: by
4 posts views Thread by TJS | last post: by
6 posts views Thread by wperry1 | last post: by
6 posts views Thread by Tony | last post: by
4 posts views Thread by Keith Bentrup | last post: by
3 posts views Thread by MikeK | last post: by
5 posts views Thread by a113n | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.