Limit <textarea> length without losing caret position

  • Thread starter Álvaro G. Vicario
  • Start date
Á

Álvaro G. Vicario

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,
 
L

Laser Lips

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
 
Á

Álvaro G. Vicario

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*
 
T

Tom Cole

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
--

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>
 
D

Dr J R Stockton

In comp.lang.javascript message <7c50f285-c43f-4f29-9d36-b4bb5ae7a922@f3
6g2000hsa.googlegroups.com>, Fri, 9 May 2008 05:07:21, Laser Lips
<[email protected]> 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.
 
Á

Álvaro G. Vicario

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.
 
P

pradeep

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
--
<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>
 
T

Thomas 'PointedEars' Lahn

pradeep said:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top