Re: Limiting character input in <textarea>
| From: | Philip Thompson | Date: | Tue, 16 Nov 2004 14:20:31 +0000 |
| Subject: | Re: Limiting character input in <textarea> | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-202143@lists.php.net to get a copy of this message | ||
On Nov 16, 2004, at 1:59 AM, Garth Hapgood - Strickland wrote:
Unlike the input fields that use maxlength to define the number of character allowed. Textarea doesnt seem to have that capability. I want to limit the number of character to 255. IS there any way of doing this without having to break my neck about it? GarthGarth, This has to be done with JavaScript or some client-side scripting language. I use this exact thing for a textarea and it works rather well. I want to restrict the user to 255 characters, so here's what I do: [code] -- in javascript -- <script type="text/javascript"><!-- function textCounter (field, countField, maxLimit) { if (field.value.length > maxLimit) field.value = field.value.substring(0, maxLimit); else countField.value = maxLimit - field.value.length; } //--></script> -- in html -- <textarea name="comments" rows="10" cols="45" onkeydown="textCounter(this.form.comments, this.form.remainingLength, 255);" onkeyup="textCounter(this.form.comments, this.form.remainingLength, 255);"></textarea> <br />Characters remaining: <input readonly type="text" name="remainingLength" size="5" maxlength="3" value="255" /> [/code] The the input text box does is just keep a counter of how much characters they have remaining. It's only function is to indicate to the user how much they have left. Hope that helps. ~Philip