SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Resource

Adding a MaxLength Attribute to TextArea

Categories...
Client User Group
Internet Explorer
Language
JavaScript
Product
IE5
Task
Components, Form Enhancement
Technology
IE5 Behaviors, Intrinsic Controls

Sponsored Links
Developer's Paradise : Inside Technique :
Adding a MaxLength Attribute to TextArea
Submission by SiteExperts Staff

Go to the resource:
Adding a MaxLength Attribute to TextArea

Add to Assistant

Short Description
An IE5 behavior (htc) that adds support for a MaxLength attribute to the TextArea element.

Long Description
The standard HTML input box has a MaxLength property that limits the length of the user's input. Matthew extends this behavior to the TextArea element by creating an IE5 behavior.

Author
Matthew Frank
Date/ Version
10/4/1999
Submission URL
http://www.SiteExperts.com/ie5/htc/ts08/page1.asp
Submission Date
Oct 4,1999
Last Update
Oct 4,1999
 

Discussion and Rate this Resource
Overall Rating: 3.8

theactivechild on Aug 19, 2007 at 7:33:27 AMRating: 5
Its Good. But a little bit explanation could have been very helpful for non programmers...( pharma career guide Career in Pharmaceutical Sales )
GlennW on Jun 20, 2007 at 6:44:30 PMRating: 4

The doKeypress function can be changed to fix the enter key issue of adding 2 characters.

function doKeypress(){
 if(!isNaN(maxLength)){
  maxLength = parseInt(maxLength);
  var oTR = element.document.selection.createRange();
  var iInsertLength = maxLength - value.length + oTR.text.length;
  var numChars = 1;
  if (event.keyCode == 13)
   numChars = 2;
  if (numChars > iInsertLength)
   event.returnValue = false;
 }
}

iInsertLength is the number of characters that can still be inserted (given the max length, current length and the number of characters that have been selected).

numChars is the number of characters that will possibly be inserted. Enter is keycode 13 so set numChars to 2. If numChars is greater than the number of characters that can be inserted then don't allow it to happen.

I haven't tested this as I don't use the 'behaviour' technique. I just use something similar to the above, using onkeypress directly from the textarea field.

MLapointe on May 5, 2006 at 2:01:55 PMNo Rating

Hi, i have try this code and it work fine, but, when you press the enter key when the have only just one character remaining, it will make the length of the text longer than the maxlength property because the enter key generate 2 characters : Carriage return and line feed.

If someone have a solution, please let me know.

Regards

Twinbee on Nov 18, 2003 at 6:14:18 PMRating: 5

I'm a bit of a newbie when it comes to HTML, but is Nobius' suggestion of using the lightweight tag in response to Chipple's code or the original article?

Also, what advantages does Chipple's code have over the original? To me, it looks like extra unnecessary code, but I'm probably wrong...

nobius on Sep 11, 2003 at 9:33:05 AMRating: 4

IMPORTANT: If you use this behavior, be sure to add the lightweight attribute on the component tag.  Otherwise IE will keep a separate copy of the behavior in memory for each element that references the behavior.  It may even download the .htc for each copy (I'm to lazy at the moment to check).  You should almost always use the lightweight unless the htc has html tags in it. Soo the tag would go from looking like

<PUBLIC:COMPONENT id="bhvMaxlength" urn="maf:Maxlength">
to
<PUBLIC:COMPONENT id="bhvMaxlength" urn="maf:Maxlength" lightweight="true">

Read more about this here http://groups.google.com/groups?selm=u%24vtYadwCHA.2532%40TK2MSFTNGP10&rnum=1

More Ratings/ Comments


To rate and comment on a resource, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password

Copyright © 1997-2008 InsideDHTML.com, LLC. All rights reserved.