Google Groups Home
Help | Sign in
Message from discussion Limit <textarea> length without losing caret position
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Álvaro G. Vicario  
View profile
 More options May 9, 8:57 am
Newsgroups: comp.lang.javascript
From: "Álvaro G. Vicario" <alvaroNOSPAMTHA...@demogracia.com>
Date: Fri, 09 May 2008 14:57:10 +0200
Local: Fri, May 9 2008 8:57 am
Subject: Re: Limit <textarea> length without losing caret position
Laser Lips escribió:

> <script type="text/javascript">
> function cutLength(e,el,max)
> {
>    if(el.value.length>max)
>    {
>            return false;
>    }else{
>            return true;
>    }
> }
[...]
> <textarea onkeypress="return cutLength(event,this,100)"></textarea> --
[...]
> ...How ever, this wont stop people cutting and pasting in over the
> limit, it will only stop people when typing normally.

It's an idea but it needs quite polishing: in Firefox, once you hit the
limit you can't use the arrow keys or delete with keyboard. And the
clipboard issue would be a problem :( Anyway, thanks for the hint.

I can think of two approaches:

1. Listening to textarea events: if an event will result in value being
changed, then cancel the event.

2. Good old "if value too large then cut value" with caret position
handling: save position, cut text and restore position.

Both look like a lot of work.. *gasp*

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google