Google Groups Home
Help | Sign in
Limit <textarea> length without losing caret position
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all
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, 6:53 am
Newsgroups: comp.lang.javascript
From: "Álvaro G. Vicario" <alvaroNOSPAMTHA...@demogracia.com>
Date: Fri, 09 May 2008 12:53:18 +0200
Local: Fri, May 9 2008 6:53 am
Subject: Limit <textarea> length without losing caret position
I need to emulate the missing "maxlegth" attribute in "textarea" fields
but all my Google searches either lead to obsolete scripts that
overwrite the "value" property (thus losing caret position) or to
complex solutions that work on top of specific frameworks.

Do you have some reference on how to do it? I'd like to make it work in
at least Firefox and IE 6 and 7.

Thank you in advance,

--
-- 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.
Laser Lips  
View profile
 More options May 9, 8:07 am
Newsgroups: comp.lang.javascript
From: Laser Lips <loudsphi...@gmail.com>
Date: Fri, 9 May 2008 05:07:21 -0700 (PDT)
Local: Fri, May 9 2008 8:07 am
Subject: Re: Limit <textarea> length without losing caret position
TRY...

<script type="text/javascript">
function cutLength(e,el,max)
{
        if(el.value.length>max)
        {
                return false;
        }else{
                return true;
        }

}

</script>
<form>
<input type="text" onkeypress="return cutLength(event,this,10)" /> --
limit to 10 characters<br/>

<textarea onkeypress="return cutLength(event,this,100)"></textarea> --
limit to 100 characters<br/>
</form>

...How ever, this wont stop people cutting and pasting in over the
limit, it will only stop people when typing normally.

Graham


    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.
Á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.
Tom Cole  
View profile
 More options May 9, 10:31 am
Newsgroups: comp.lang.javascript
From: Tom Cole <tco...@gmail.com>
Date: Fri, 9 May 2008 07:31:37 -0700 (PDT)
Local: Fri, May 9 2008 10:31 am
Subject: Re: Limit <textarea> length without losing caret position
On May 9, 8:57 am, "Álvaro G. Vicario"

This doesn't work?

<script type="text/javascript">
function getCaretPosition (ctrl)  {
        var caretPos = 0;
        if (document.selection) {
                ctrl.focus ();
                var Sel = document.selection.createRange ();
                Sel.moveStart ('character', -ctrl.value.length);
                caretPos = Sel.text.length;
        }
        else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
                caretPos = ctrl.selectionStart;
        }
        return caretPos;

}

function setCaretPosition(ctrl, pos) {
        if(ctrl.setSelectionRange)      {
                ctrl.focus();
                ctrl.setSelectionRange(pos,pos);
        }
        else if (ctrl.createTextRange)  {
                var range = ctrl.createTextRange();
                range.collapse(true);
                range.moveEnd('character', pos);
                range.moveStart('character', pos);
                range.select();
        }

}

function checkMax(element, max) {
    var caretpos = getCaretPosition(element);
        var value = element.value;
        if (value.length > max) {
                value = value.substring(0, max);
        }
        element.value = value;
        setCaretPosition(element, caretpos);
}

</script>
...
<textarea cols="5" rows="50" onkeyup="checkMax(this, 25);"></textarea>

    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.
Dr J R Stockton  
View profile
 More options May 9, 5:35 pm
Newsgroups: comp.lang.javascript
From: Dr J R Stockton <j...@merlyn.demon.co.uk>
Date: Fri, 9 May 2008 22:35:26 +0100
Local: Fri, May 9 2008 5:35 pm
Subject: Re: Limit <textarea> length without losing caret position
In comp.lang.javascript message <7c50f285-c43f-4f29-9d36-b4bb5ae7a922@f3
6g2000hsa.googlegroups.com>, Fri, 9 May 2008 05:07:21, Laser Lips
<loudsphi...@gmail.com> posted:

The advice of anyone who posts this code

>function cutLength(e,el,max)
>{
>       if(el.value.length>max)
>       {
>               return false;
>       }else{
>               return true;
>       }
>}

rather than

function cutLength(e, el, max) { return el.value.length <= max }

is unlikely to be worth considering.

--
(c) John Stockton, nr London UK.  ?...@merlyn.demon.co.uk  Turnpike v6.05  MIME.
 Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
     Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.


    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.
Álvaro G. Vicario  
View profile
 More options May 13, 6:51 am
Newsgroups: comp.lang.javascript
From: "Álvaro G. Vicario" <alvaroNOSPAMTHA...@demogracia.com>
Date: Tue, 13 May 2008 12:51:15 +0200
Local: Tues, May 13 2008 6:51 am
Subject: Re: Limit <textarea> length without losing caret position
Tom Cole escribió:
> This doesn't work?

> <script type="text/javascript">
> function getCaretPosition (ctrl)  {
>    var caretPos = 0;

[...]

It works badly in IE 6. However, it contains some good ideas. If I can't
find a prewritten script and I have to write mine, I'll take them into
account.

--
-- 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.
pradeep  
View profile
 More options May 20, 8:42 am
Newsgroups: comp.lang.javascript
From: pradeep <pradeep.psg...@gmail.com>
Date: Tue, 20 May 2008 05:42:56 -0700 (PDT)
Local: Tues, May 20 2008 8:42 am
Subject: Re: Limit <textarea> length without losing caret position
On May 13, 3:51 pm, "Álvaro G. Vicario"

<script>
function textLimit(field, maxlen)
{
  if (field.value.length > maxlen)
  {
    field.value = field.value.substring(0, maxlen); // if the length
is more than the limit (maxlen) then take only the first 'maxlen' no.
of characters
  }
  return (maxlen - field.value.length);   // current value length is
returned back
}

</script>

<textarea onkeyup='textLimit(this,100);return false'></textarea>


    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.
Thomas 'PointedEars' Lahn  
View profile
 More options May 20, 9:21 am
Newsgroups: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Tue, 20 May 2008 15:21:17 +0200
Local: Tues, May 20 2008 9:21 am
Subject: Re: Limit <textarea> length without losing caret position

pradeep wrote:
> <script>

  <script type="text/javascript">

> function textLimit(field, maxlen)
> {
>   if (field.value.length > maxlen)
>   {
>     field.value = field.value.substring(0, maxlen); // if the length
> is more than the limit (maxlen) then take only the first 'maxlen' no.
> of characters
>   }
>   return (maxlen - field.value.length);   // current value length is
> returned back
> }
> </script>

> <textarea onkeyup='textLimit(this,100);return false'></textarea>

| but all my Google searches either lead to obsolete scripts that
| overwrite the "value" property (thus losing caret position)

PointedEars
--
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann


    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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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