[thelist] Re: Text area question

John.Brooking at sappi.com John.Brooking at sappi.com
Mon Mar 15 12:21:40 CST 2004


>Date: Mon, 15 Mar 2004 10:39:55 -0600
>From: Tanner Burson <tanner at younet.okstate.edu>
>To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
>Subject: Re: [thelist] Re: Text area question
>
>>>>Is there any way to limit the number of characters in a 
>>>><textarea></textarea> field?
>>
>>I haven't tried this, but couldn't you just add an onKeyPress (?) event to
>>check the current length and return false from the event if the limit has
>>been reached? Or something like that?
>>
>Well you could use the onKeyPress to check the length, but you couldn't 
>preven the keypress from occuring (I don't believe), so you'd actually 
>end up having to delete the last character each time.  Which is by no 
>means impossible, or even that big of a challenge, but I am curious as 
>to what the solution in this zip file is, if it is different than this.

No, returning a false value to most events *does* usually prevent the
default processing from occurring. The following tag does exactly what the
original poster wanted (if that the maximum characters desired is 50):

   <textarea onKeyPress="return ( this.value.length < 50 );"></textarea>

This works fine on my IE 6.0 browser, and I'm pretty sure it should work on
other standard ones too. Granted, it is JavaScript (which, as has been noted
many times on this list, you shouldn't assume is available), and it's
arguably a hack, but nevertheless, it does work. Note that the fact that JS
may not be available, as well as (more importantly) security factors, mean
that the server still shouldn't assume anything about the input, including
its maximum length.

- 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