473,465 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Limit input length in textarea

Hi all,

I find a JavaScript that limits the field length in a textarea, but it
is not completed.

The script does not handle the copy and paste case. For example, says
the textarea field length is limited to 5 and a 6-character length
text is pasted, no event is triggered. (The script only handles
onKeyPress and onKeyUp)

It would be grateful if you could enlighted me on how to capture the
paste event in a textarea.

Thanks in advance for any inputs and ideas.

Wing

<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document
-->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<!-- textCounter() parameters are: text field, the count field, max
length -->

<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up
to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4
onKeyDown="textCounter(this.form.message,this.form .remLen,125);"
onMouseOut="textCounter(this.form.message,this.for m.remLen,125);"
onKeyUp="textCounter(this.form.message,this.form.r emLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125">
characters left</font>
</form>
</center>
<!-- Script Size: 1.37 KB -->
Jul 20 '05 #1
4 29479
Change the event handlers to onChange within the text area.
"wing" <wi******@witty.com> wrote in message
news:87**************************@posting.google.c om...
Hi all,

I find a JavaScript that limits the field length in a textarea, but it
is not completed.

The script does not handle the copy and paste case. For example, says
the textarea field length is limited to 5 and a 6-character length
text is pasted, no event is triggered. (The script only handles
onKeyPress and onKeyUp)

It would be grateful if you could enlighted me on how to capture the
paste event in a textarea.

Thanks in advance for any inputs and ideas.

Wing

<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document
-->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<!-- textCounter() parameters are: text field, the count field, max
length -->

<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up
to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4
onKeyDown="textCounter(this.form.message,this.form .remLen,125);"
onMouseOut="textCounter(this.form.message,this.for m.remLen,125);"
onKeyUp="textCounter(this.form.message,this.form.r emLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125">
characters left</font>
</form>
</center>
<!-- Script Size: 1.37 KB -->

Jul 20 '05 #2
Hi Steve,

Thanks for your comments,

I have tried the onChange event handler and found the followings
issues.
1. the event is not called in the mouse paste action.
2. sometimes the event is not called when I click the mouse outside of
the textarea.

Am I missing something here?

I think the primary goal to capture the mouse paste action. Is this
possible with JavaScript?

Cheers,

Wing
Jul 20 '05 #3
Dear all,

Is there another way to limit the size text of a textarea?

Just some brain storming ideas for discussion.
- using java applet
- using flash

Thanks in advance for your inputs and ideas.

Wing
wi******@witty.com (wing) wrote in message news:<87*************************@posting.google.c om>...
Hi Steve,

Thanks for your comments,

I have tried the onChange event handler and found the followings
issues.
1. the event is not called in the mouse paste action.
2. sometimes the event is not called when I click the mouse outside of
the textarea.

Am I missing something here?

I think the primary goal to capture the mouse paste action. Is this
possible with JavaScript?

Cheers,

Wing

Jul 20 '05 #4
danjam
7 New Member
Here's a script that'll do the job for you, including copy-paste issues:

http://www.webonweboff.com/widgets/js/text_counter.aspx

Hope this helps...
Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jay | last post by:
I have an HTML form with a textarea. When passing large amount of data in the textarea, the Processing asp pages catch an error message as follows: Error Type: (0x80020009) Exception occurred. ...
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...
1
by: cw | last post by:
Hi all, Is anyone have an idea that limit message length input by user into textbox. I'hv tried the maxlength property by setting it to 150, but it seems like not working.., regards
5
by: JohnSouth | last post by:
Hi In a TextBox I can set a maximum size for the entry. Is there a way of limiting the number of characters being entered into a TextArea control? John South www.wherecanwego.com
6
by: Vortexmind | last post by:
Hi all I was wondering if this is possible in Javascript. I want to make a bookmarklet. When a user launches it, it tells the user to select an element in the page (for example a textarea) with...
1
by: anon1m0us | last post by:
Hi; I am waaayyy new at ASP so I am hopping someone can help me. I wrote an ASP that accepts input from a user. I need the input saved in different cells in an excel sheet. Everytime someone hits...
7
Haitashi
by: Haitashi | last post by:
This is in the head: <script language="javascript" type="text/javascript"> function limitText(limitField, limitCount, limitNum) { if (limitField.value.length > limitNum) { limitField.value =...
3
by: teser3 | last post by:
I have the below that limits the textarea input to 500 characters but cant get the alert message to work. It doesnt show anything. Please advise. <script language="javascript"...
7
by: =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
I need to emulate the missing "maxlegth" attribute in "textarea" fields but all my Google searches either lead to obsolete scripts that overwrite the "value" property (thus losing caret position)...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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...
0
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 ...

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.