[thelist] Re:TIP: Limiting TextArea length

John.Brooking at sappi.com John.Brooking at sappi.com
Thu Mar 18 08:54:44 CST 2004

>Date: Wed, 17 Mar 2004 11:41:34 -0800
>From: "James Aylard" <evolt at pixelwright.com>
>To: <thelist at lists.evolt.org>
>Subject: Re: [thelist] TIP: Limiting TextArea length
>    For clarification, you don't need to check the clipboard. The onpaste
>event fires when the paste occurs, allowing you to check the length of the
>textarea value and truncate it if it exceeds the allowed limit.

   Not to pick nits, but what exactly does "when" mean, in the sentence "the
event fires *when* the paste occurs" -- before, after, or simultaneous to
the paste? My impression, which seems supported by the testing I did in IE
6, is that the event occurs at the point where the system is just about to
put the characters into the field, but hasn't yet. Checking the length at
that point and returning false, if necessary, prevents the system from
continuing with the paste at all, which is just what I intended. Maybe that
is not the same as someone else would intend, to which your next point

>     When I worked with this a couple of years ago, I found problems with
>checking the textarea value immediately, and so I put in a 10ms setTimeout
>before validating the length of the entry -- effectively creating an
>onafterpaste event.

   So it sounds like your setTimeout allowed the onPaste to continue putting
the text in, then by the time the timer went off, the onPaste event had
finished and the text was inserted, so you examined it and truncated as
necessary. That works too. But I think (haven't tested) I could do the same
thing without the setTimeout by examining the clipboard and truncating its
extra characters before allowing the paste to continue. Same net, judgement
call as to which is preferable.

   As the Perl mongers say -- TMTOWTDI: There's More Than One Way To Do It.

>Another trick for IE-
>only environments is to take advantage of so-called expando properties (or
>user-defined properties) for html elements. You can create a "maxlength"
>property for the textarea, and reference that property in your scripts
>the getAttribute() method). That is especially flexible in environments
>you have more than one textarea with more than one length-limit value.

   Yet Another Way (YAW?) - thanks.

- John


This message may contain information which is private, privileged or
confidential and is intended solely for the use of the individual or entity
named in the message. If you are not the intended recipient of this message,
please notify the sender thereof and destroy / delete the message. Neither
the sender nor Sappi Limited (including its subsidiaries and associated
companies) shall incur any liability resulting directly or indirectly from
accessing any of the attached files which may contain a virus or the like. 

More information about the thelist mailing list