473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I limit number of characters in textarea outside of form tag?

Haitashi
96 New Member
This is in the head:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. function limitText(limitField, limitCount, limitNum) {
  3.     if (limitField.value.length > limitNum) {
  4.         limitField.value = limitField.value.substring(0, limitNum);
  5.     } else {
  6.         limitCount.value = limitNum - limitField.value.length;
  7.     }
  8. }
  9. </script>
This is the form:
Expand|Select|Wrap|Line Numbers
  1. <form name="myform">
  2. <textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
  3. onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
  4. </textarea><br>
  5. <font size="1">(Maximum characters: 100)<br>
  6. You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>
  7. </form>
The issue is that the textarea where I plan to use this script doesn't live inside a form tag. And, as you can see, one of the arguments being passed in is the "this.form" .

I need the form info for the ELSE of the function. Any suggestions? Adding a form tag is not recommended for me at the moment.

THANKS!
Apr 3 '08 #1
7 2153
hsriat
1,654 Recognized Expert Top Contributor
Instead of making the referance as this.form.limit edtextarea, you can just write this, because you are inside that textarea tag (limitedtextarea).

And for the countdown text, you can use document.getEle mentById('count down') (for this you need to add id="countdown" to the countdown input tag)


Regards,
Harpreet
Apr 3 '08 #2
Murdz
34 New Member
A textarea should always be inside a <form> tag.
Apr 4 '08 #3
hsriat
1,654 Recognized Expert Top Contributor
A textarea should always be inside a <form> tag.
Not a mandatory requirement!
Apr 4 '08 #4
Haitashi
96 New Member
Yeah, I knew it wasn't mandatory. In the way our application is created, there are specific reasons why they placed this field outside a form tag.

Anyways. I made the suggested changes. My field looks like this:
Expand|Select|Wrap|Line Numbers
  1.     <textarea name="creditNotes" onKeyDown="limitText(this.creditNotes,document.getElementById('countdown'),100);" onKeyUp="limitText(this.creditNotes,document.getElementById('countdown'),100);" id="creditNotes" cols="40" rows="6" style="width:324px; height:82px;"></textarea></div>
  2.             <font size="1">(Maximum characters: 100)<br>
  3.             You have <input readonly type="text" id="countdown" name="countdown" size="3" value="100"> characters left.</font>
The script looks the same as before. However, I an error that states that limitField has no properties. "limitField " should contain the value of the "creditNote s" field.
Apr 4 '08 #5
hsriat
1,654 Recognized Expert Top Contributor
Do this change:
Expand|Select|Wrap|Line Numbers
  1. <textarea name="creditNotes" onKeyDown="limitText(this, document.getElementById('countdown'), 100);" onKeyUp="limitText(this, document.getElementById('countdown'), 100);" id="creditNotes" cols="40" rows="6" style="width:324px; height:82px;"></textarea>
  2.  
  3. </div>
  4.  
  5. <font size="1">(Maximum characters: 100)<br>
  6. You have <input readonly type="text" id="countdown" name="countdown" size="3" value="100"> characters left.</font>
Apr 4 '08 #6
Haitashi
96 New Member
It worked! Thank you very very much Harpreet! ^_^
Apr 4 '08 #7
hsriat
1,654 Recognized Expert Top Contributor
You are welcome. :)

You know when you write this inside a tag, you refer to that particular tag. So there was no need of this.creditNote s. That would return nothing.


Regards,
Harpreet
Apr 4 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
23082
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.
4
29538
by: wing | last post by:
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)
5
2652
by: yawnmoth | last post by:
say i have a form where, if a user clicks in it, all the text is highlighted, and where, if a user clicks on a button outside of the form, a certain text string is inserted where the cursor in the textarea is. why is it, then, that when i, immediatly after loading the page, click in the textarea, highlighting the text, click again in the middle of the text (effectively unhighlighting everything, as well), and then click on the button to...
5
12157
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
6
11640
by: tony wong | last post by:
i wish to count number of characters in textarea box during typing in and display it somewhere in html page. so the number will increase during writing. is it possible to do it by javascript? Thanks a lot. Tony
5
2224
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
3488
by: @sh | last post by:
Guys, Working on a function to alert the user to too many characters being entered into a text area, I've put together this function so far borrowing bits from resource websites... function Ash_LimitMaxTextAreaCharacters(TheMaxCharacters,TheFriendlyFormName) { if(document.myform.box.value.length > TheMaxCharacters) { alert('Sorry but '+TheFriendlyFormName+' contains too many characters,
3
4205
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
3116
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
9663
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
9511
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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
10136
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
9016
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
7525
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...
1
4090
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
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
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.