473,232 Members | 1,571 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,232 software developers and data experts.

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

Haitashi
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 2096
hsriat
1,654 Expert 1GB
Instead of making the referance as this.form.limitedtextarea, you can just write this, because you are inside that textarea tag (limitedtextarea).

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


Regards,
Harpreet
Apr 3 '08 #2
Murdz
34
A textarea should always be inside a <form> tag.
Apr 4 '08 #3
hsriat
1,654 Expert 1GB
A textarea should always be inside a <form> tag.
Not a mandatory requirement!
Apr 4 '08 #4
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 "creditNotes" field.
Apr 4 '08 #5
hsriat
1,654 Expert 1GB
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
It worked! Thank you very very much Harpreet! ^_^
Apr 4 '08 #7
hsriat
1,654 Expert 1GB
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.creditNotes. 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
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. ...
4
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...
5
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...
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...
6
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? ...
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: @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...
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)...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.