limiting textarea input

They have: 16 posts

Joined: Jan 2000

Is there a way to limit the lenght (characters) for a textarea form element? 'Maxlength' only seems to work for input tags such as text.

They have: 1,587 posts

Joined: Mar 1999

um, u've confused me. ur asking how to limit the characters for a form text area. then u go on to talk about maxlength, hello. maxlength is it, i'm confused, hehe.

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

They have: 16 posts

Joined: Jan 2000

The following form tags are for different form elements:

The tag creates a scrolling text box. 'maxlength' does not work with this tag.

My question is, how can I limit input into a field the same way 'maxlength' does with a tag.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

maxlength="235" or whatever limits the amount of characters that can be entered into the form field.

In order to limit the size of the form field, you use size="10" where 10 is whatever size works for your needs.

Please keep in mind that Netscape will make the field sizes different than IE will, so test in both.

Smiling Suzanne

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

size=10 is only for
rows=20 cols=4 is for

as for limiting characters... let me get my book..
there is no property for that.

BUT, if your adventurous, you could write a javascript for this using the onKeyPress or onKeyDown.
count the number of characters
once over the limit, alert() or something.

Mark Hensler
If there is no answer on Google, then there is no question.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

doh! Max is right, I was responding to the form element.

Max is also right that the element has no natural limitation attributes other than size (rows, cols), so if you want to limit characters, either use JavaScript, trim using your form parser in whatever language you script in (ASP, Perl, et cetera), or use an element instead.

Smiling Suzanne

They have: 74 posts

Joined: Sep 2000

Andrew,
I gpot this code from The Javascript Source.
In this case the maxlength is 125.
You can of course change that to whatever you want,
just follow the directions:

<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

&lt;script LANGUAGE="JavaScript"&gt;
<!-- Original:  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source -->

<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: <a href="http://www.shiningstar.net" class="bb-url">http://www.shiningstar.net</a> -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! <a href="http://javascript.internet.com" class="bb-url">http://javascript.internet.com</a> -->

<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
&lt;/script&gt;
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<!-- textCounter() parameters are:  text field, the count field, max length -->

<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>

<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  1.37 KB -->
'
Hope this helps.
Ian,

hehe

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

thanks!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.