473,575 Members | 3,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2437
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.c omwrote:
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.getEle mentById('test' ).value;
if (value.length 20) {
document.getEle mentById('test' ).value = value.substring (0, 20);
}
}

<textarea id="test" onkeyup="testLe ngth();"></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.javas cript:
On Apr 26, 6:32 am, lamp <lam...@gmail.c omwrote:
>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.getEle mentById('test' ).value;
if (value.length 20) {
document.getEle mentById('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='testLe ngth(this);'></textarea>
=============== =============== =============
>
<textarea id="test" onkeyup="testLe ngth();"></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.len gth 20)
test.value = textboxtextSave
else
textboxtextSave = test.value;
};
</script>

<textarea onkeyup='testLe ngth(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.c omwrote:
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.c omwrote:
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="textarean ame" onkeyup="return checknumber()"
onfocus="checkn umber()" onblur="checknu mber()"></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.formna me.textareaname .value.length;

if (numChars maxChars)
{
document.formna me.textareaname .value =
document.formna me.textareaname .value.substrin g(0,maxChars);
numChars = maxChars;
document.formna me.textareaname .focus();
r = false;
}
else {
r = true;
}

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

document.formna me.remaining.va lue = 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
16675
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, then a generic element's font will do OK, too. What's the correct way to do this, please (so that it will also work for IE 6)? The motivation for...
1
3554
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
12132
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 keyboard (keypress, keyup events) * Must work on pasted input from context menu * Must work on pasted input via CTRL+V and similar * Must work on...
4
4240
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
5972
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 think my way around. I am using a <textarea> to display the code but some of my scripts have <textarea> tags in them and when the </textarea> tag...
6
31429
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 the HTML 4.01 specification I see this, too. What I'm wondering is - 'cols' & 'rows' determines the height & width of a textarea. So shouldn't that...
4
7756
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 find the text and select it using the function below, BUT I can't figure out how to have the textarea automatically scroll to the selection in...
3
4922
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 indicate that it does but I can't get it to work for the life of me. I've tired everything I can think of to try get notifications for the events: ...
3
5632
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 replace spaces with &nbsp;. The <br> part works well enough, but I keep getting "%20" instead of "&nbsp;" for the spaces. I understand that escape()...
5
4765
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 test="@READONLY"> <xsl:attribute name="READONLY"><xsl:text>READONLY</xsl:text></ xsl:attribute> </xsl:if> <xsl:value-of select="."/> </TEXTAREA>
0
7845
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8121
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8286
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6517
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5340
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2287
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.