473,661 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ 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="JavaS cript">
<!-- Begin
function textCounter(fie ld, countfield, maxlimit) {
if (field.value.le ngth > maxlimit) // if too long...trim it!
field.value = field.value.sub string(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.valu e = maxlimit - field.value.len gth;
}
// 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="text Counter(this.fo rm.message,this .form.remLen,12 5);"
onMouseOut="tex tCounter(this.f orm.message,thi s.form.remLen,1 25);"
onKeyUp="textCo unter(this.form .message,this.f orm.remLen,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 29511
Change the event handlers to onChange within the text area.
"wing" <wi******@witty .com> wrote in message
news:87******** *************** ***@posting.goo gle.com...
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="JavaS cript">
<!-- Begin
function textCounter(fie ld, countfield, maxlimit) {
if (field.value.le ngth > maxlimit) // if too long...trim it!
field.value = field.value.sub string(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.valu e = maxlimit - field.value.len gth;
}
// 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="text Counter(this.fo rm.message,this .form.remLen,12 5);"
onMouseOut="tex tCounter(this.f orm.message,thi s.form.remLen,1 25);"
onKeyUp="textCo unter(this.form .message,this.f orm.remLen,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.goo gle.com>...
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
23071
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. The error is generated once the trim or length or replace function is called on the field when long text is passed in the textarea.
5
12133
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 pasted input via browsers menu>Edit>Paste * Must work in Mozilla + IE and coded via W3C standards
1
6142
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
2197
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
1811
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 the mouse. When the user clicks on the textarea, my script gets a reference of the element that he clicked so i can operate on it. Is it possibile? Thanks for any input on this, I have some difficulties sorting my ideas out :)
1
2474
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 Submit, it should go to a new row. The code below shows how I want the form set up. I have no clude on how to get the input and save it on a server's D:\Drive. ANy help is greatly appreciated!!!!
7
2119
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 = limitField.value.substring(0, limitNum); } else { limitCount.value = limitNum - limitField.value.length; } } </script>
3
4185
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" type="text/javascript"> function limitText(limitField, limitCount, limitNum) { if (limitField.value.length limitNum) { limitField.value = limitField.value.substring(0, limitNum); alert("You are trying to enter more than 500 characters"); } else {
7
3074
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) or to complex solutions that work on top of specific frameworks. Do you have some reference on how to do it? I'd like to make it work in at least Firefox and IE 6 and 7. Thank you in advance,
0
8432
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.