Max length for textarea

liunx

Guest
Sorry if this is a duplicate. I didn't see my previous thread posted.<br />
<br />
---------<br />
<br />
Hello,<br />
<br />
What do I need to do to dynamically keep track of the length of text for textarea? Once the max length is hit, the user should not be able to enter anymore text. This way it is a visual cue for the user that they need not waste time entering in more text. And they need not have to wait until they submit the form to realize they have exceeded the limit.<br />
<br />
Is there a solution to do this that is supported on IE and Netscape?<br />
<br />
I know textarea does not support maxlength.<br />
Is there a way to use text input allowing for multiline input?<br />
<br />
Thank you,<br />
Katherine<!--content-->Hi Katherine!<br />
<br />
Add maxlength tag, for example:<br />
<textarea name="yrmsg" rows="5" cols="40" maxlength="200"></textarea><br />
<br />
maxlength="200" is for 200 characters.<!--content-->Ok heres the fix:<br />
<br />
In your head tag:<br />
<script><br />
function txtCnter(field, countfield, maxlimit) {<br />
if (field.value.length > maxlimit) // if too long...trim it!<br />
field.value = field.value.substring(0, maxlimit);<br />
// otherwise, update 'characters left' counter<br />
else <br />
countfield.value = maxlimit - field.value.length;<br />
} <br />
</script><br />
<br />
In your BODY somewhere:<br />
<textarea name="message1" cols="50" rows="20" onKeyDown="txtCnter(this.form.message1,document.forms[1].remLen5,255);" onKeyUp="txtCnter(this.form.message1,document.forms[1].remLen5,255);"></textarea><br />
<br />
and another form (DOM forms[1]) w/ hidden field:<br />
<input readonly type="hidden" name="remLen5" size="3" maxlength="3" value="255"> <br />
<br />
The 255 value is whatever you want to set the max num of characters to.<br />
<br />
One of the good JS sites out there has the orginal code to this, I just cant remember where I got it from (or if someone sent it to me). But there are several ways of limiting the text, BUT they all require Javascript!!<br />
<br />
hth<br />
Mark<br />
<br />
[Edited by Marlboro on 03-06-2001 at 12:44 PM]<!--content-->eforms, <br />
I wish the solution was that simple...but that didn't work. I believe Marlboro is correct by using onkeyup and checking length each time there in an entry.<!--content-->Thank you all for your responses.<br />
<br />
Checking user input this way will certainly accomplish the task. What is performance impact of doing it this way? Better off just to wait until form is submitted? Marlboro, you mentioned solutions like this require Javascript. What are disadvantages?<br />
<br />
Regards,<br />
Katherine<!--content-->disadvantages are that users have to have JS enabled to see the effect. Most do anyway as JS is commonly used on most web pages for verifications just like this. Also, it will check the length of the text field each time you input a character. The actual computer time it takes to do this will be minimal...but on slower computers it may appear that the computer renders the text a little slow. In reality, you shouldn't see any performance hit. <br />
Alternatives:<br />
You could check the text field for length when the users cursor leaves it. This would not let the user continue until the conditions were satisfied. This way, one check would be performed....instead of a seperate check for each entry.<!--content-->
 
Back
Top