473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2423
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Csaba Gabor | last post by:
What I'd like to do is to be able to set the font of a textarea element to the same font that another element is using (say, for example, an <INPUT type=text ...> element, but if that's a no go,...
1
by: Volt | last post by:
is there any way to select and mark part of text in textarea by regular expression? i need to select the first string in textarea whitch is like xxxxx,xxx where x is any character
5
by: Jesper Rønn-Jensen | last post by:
I have a textarea that must be limited to 70 characters. No big deal -- at least so I thought. * Textarea must not exceed 70 characters * Exceeding content must be cut off * Must work on input by...
4
by: TJS | last post by:
can the rows and columns of a textarea element with an id be changed on the fly ? if so is there an example ?
6
by: wperry1 | last post by:
I am writing a small database utility to catalog all of my favorite ASM/JS/VBS... functions and scripts on an asp page. Everything is going smoothly except for one thing that I can't quite seem to...
6
by: Tony | last post by:
The w3schools HTML tag reference for <textarea> http://www.w3schools.com/tags/tag_textarea.asp says that the attributes 'cols' and 'rows' are REQUIRED attributes for the textarea tag. Looking at...
4
by: Keith Bentrup | last post by:
Hi all, I wrote a simple search function to find text in a textarea where not all the text is visible (ie. the text box displays 10 lines but there may be more than 1000 lines to search). I can...
3
by: MikeK | last post by:
Ok, I've been noodling with this for several days now and I'm starting to go crazy. Does Apple's Safari browser support drag events on Textarea elements? The few specs and docs I've found seem to...
3
by: FunkHouse9 | last post by:
I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to...
5
by: a113n | last post by:
I have the following XSL code to handle textareas: <!-- Match TEXTAREA --> <xsl:template name="TEXTAREA" match="TEXTAREA"> <TEXTAREA ROWS="{@ROWS}" COLS="{@COLS}" NAME="{@NAME}"> <xsl:if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.