Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit Textarea chars. and dynamic textfield 2

Status
Not open for further replies.

rmz8

Programmer
Aug 24, 2000
210
US
How can I make it so that I can limit the amount of characters within a textarea and make a small text field that shows how many characters are currently typed in the field?

Ryan ;-]
 
<script>
function func(obj, num, othobj)
{
str = new String(obj.value);
othobj.value = new Number(str.length + 1);
dnum = num -1;
if(str.length >= dnum)
{
alert('you can only have '+num+' characters in this field.');
obj.onfocus = obj.blur;
}
}
</script>
<form name=&quot;frm&quot;>
<input type=&quot;text&quot; name=&quot;textbox&quot;>
<textarea onkeypress=&quot;func(this, 30, document.frm.textbox)&quot;></textarea>
</form>

this works for me... adam@aauser.com
 
That only works when a key is pressed...in other words, if something is copied and pasted, it won't work. If I change the event to OnChange, it works, except only when I Blur to textarea. Any suggestions?

Ryan ;-]
 
I was just saying that it doesn't count the text that would be entered from a copy/paste.

Ryan ;-]
 
hey, couldn't you just use the maxchar=30:

<input type=text maxchar=30 name=&quot; text Box With Only Thirty Characters&quot;>


then, also, use luciddream's function.
theEclipse
eclipse_web@hotmail.com
robacarp.webjump.com
**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
This is a text area--that is not one of the options, I believe.

Ryan ;-]
 
But how can you make it so that it places the amount of characters in the textbox if there is already a value in the box? For example, I want the textbox to show how many chars. are in the textarea when the page is loaded (as well as how many when a char. is typed) but the text field doesn't reflect the initial value of chars. in the textarea. Do I want to use an OnLoad?

Ryan ;-]
 
This code doesn't work in NS, however. It says othobj is undefined.

Ryan ;-]
 
because &quot;othobj&quot; is not &quot;document.frm.textbox&quot; in ns !!!! in another of your threads you have the answer on how to refer to form elements in ns
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top