473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
--
Jun 27 '08 #1
7 3052
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
Jun 27 '08 #2
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
--
Jun 27 '08 #3
On May 9, 8:57*am, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
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>

Jun 27 '08 #4
In comp.lang.javascript message <7c50f285-c43f-4f29-9d36-b4bb5ae7a922@f3
6g2000hsa.googlegroups.com>, Fri, 9 May 2008 05:07:21, Laser Lips
<lo*********@gmail.composted:

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.
Jun 27 '08 #5
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
--
Jun 27 '08 #6
On May 13, 3:51 pm, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
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>
Jun 27 '08 #7
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
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Augustus | last post by:
Hiya, I have a form with a <textarea></textarea> to receive user input. This input is then stored in a database and sent by fax... I need to be able to remove the carriage returns (enter...
4
by: Dennis Allen | last post by:
Hi. I hope someone here can help. I'm webmaster for a local astronomy club. Just went over our web site. Have validated every htm file on the site except:...
5
by: Jesper Rønn-Jensen | last post by:
I have a textarea that must be limited to 70 characters. No big deal -- at least so I thought. * Textarea must not exceed 70 characters * Exceeding content must be cut off * Must work on input by...
3
by: Bart Van der Donck | last post by:
Hello, I have a dynamic page of which I don't know how many forms will be on it, neither which and how many elements will be in each form. I use the following java script to disable all...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
3
by: Jarek Mielcarek | last post by:
hi all, in xml file I have some fields which are source for <textarea> element. I'd like to transform this file using xslt and set the rows property of <textarea> depend of lines in some source...
2
by: Michael | last post by:
Question 1 ---------------- I am writing an advanced BBCode system for my forums and I would like to be able to find where the cursor was positioned last in the text so I could insert the BBCode...
6
by: Tony | last post by:
The w3schools HTML tag reference for <textarea> http://www.w3schools.com/tags/tag_textarea.asp says that the attributes 'cols' and 'rows' are REQUIRED attributes for the textarea tag. Looking at...
3
by: FunkHouse9 | last post by:
I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.