Greetings,

It is often very convenient to have a maximum length feature for textareas as well. Standard HTML texarea doesn't have such feature but it can be implemented using javascript. I've found a thread in Drupal dedicated to this problem and improved proposed solution. It works nice now.
Here is the description with source code http://drupal.org/node/80122#comment-200738

My suggestion is to take it into Drupal core and implement as #maxlength attribute. Javascript can be put into textarea.js which is already loaded in theme_textarea($element). The only thing is to handle #maxlength attribute correctly. I also think that you will rework my version of textareaMaxLength() function using jQuery and make it much better.

Thanks.

Comments

ardas’s picture

This is of course not a good solution, because there is a problem with copy/cut/paste functions and we need much more advanced logic to limit text in textareas. OnKeyPress event isn't a good choice fo this because a length of inserted text is not known in OnKeyPress ... but, nevertheless, I didn't find a better javascript solution for now.

I think that grippie div can be used as a 'textarea status bar' where length countdown meter can be placed...

The original idea was to implement this feature in a Drupal core mostly because this feature is even more valuable than resizing.

aries’s picture

aries’s picture

aries’s picture

StatusFileSize
new873 bytes

Here's my modificated maxlength (the original has made by Bjorn Wijers) jQuery code. Currently is only works for one textarea per page.

aries’s picture

Status: Active » Needs review
ChrisKennedy’s picture

Version: 5.x-dev » 6.x-dev
Status: Needs review » Needs work

This needs major work in all areas: general implementation, coding style (http://drupal.org/node/318), translation support, etc. It will need to be for Drupal 6. See http://drupal.org/patch too.

ChrisKennedy’s picture

This needs major work in all areas: general implementation, coding style (http://drupal.org/node/318), translation support, etc. It will need to be for Drupal 6. See http://drupal.org/patch too.

ChrisKennedy’s picture

Title: Implement #maxlength for textareas » Implement & dynamically validate textarea #maxlength
Status: Needs work » Needs review

Here is a basic implementation.

ChrisKennedy’s picture

StatusFileSize
new3.19 KB

Patch.

ChrisKennedy’s picture

Title: Implement & dynamically validate textarea #maxlength » Implement+dynamically validate textarea #maxlength
ChrisKennedy’s picture

StatusFileSize
new2.85 KB

Re-rolled and cleaned up. You can try it out by adding a maxlength to the comment textarea in comment_form().

jrabeemer’s picture

We need an interface in CCK add textarea field to set the limit. :-p

catch’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs review » Needs work

Nice idea but no longer applies, and will have to wait for D7.

ChrisKennedy’s picture

Status: Needs work » Needs review
StatusFileSize
new2.74 KB

Here is a re-roll.

ChrisKennedy’s picture

Title: Implement+dynamically validate textarea #maxlength » Validate textarea #maxlength

Anyone care to review?

ardas’s picture

I'm glad that someone rewrote my proposed script which was very poor.
This patch is worth to be included into the core but this is what we can't do here :)
Need to engaged someone from the Drupal core...

ardas’s picture

CCK textarea already has a maxlength field but it is ignored since there is no support of this feature in the textarea element.

pasqualle’s picture

subscribe

floretan’s picture

StatusFileSize
new3.9 KB

Works as described, but using the maxlength attribute breaks XHTML validation.

I updated the patch from #14 to store the maxlength of a textarea in the javascript variable Drupal.settings.textarea_maxlength which is an associative array using the textarea id as the key. The character counter has also been simplified and moved to the upper-right corner of the textarea, with a "warning" class added to it when the limit is reached.

chx’s picture

This would enforce a maxlength on the number of characters inputted into a textarea. That's hardly the Drupal way. We have nice filters and the output can be very different in length vs the input. Even if the textarea were aware of its filter and run check_markup on it, you can never know with a non-caching filter how it will change in time, per user, per page or whatever... I would not like to hurt anyone's feelings here but I am inclined to won't fix this.

To give you an example, if I have a filter which changes [img=...] into a picture then that's how many characters...?

Edit: textfields are rarely (I dare to say, almost never) ran through check_markup so the problem does not exist there.

floretan’s picture

I must admit that I hadn't considered the issue of character limits combined with input filters. In the current state, I agree that this would be a "won't fix". However, if Gabor's suggestion to associate input formats with textareas using some #formatter FAPI property, it would be possible to only look at #maxlength if we are dealing with a plaintext field.

Limiting the character count is also doable at the filter level, and if the #formatter property is implemented, a direct feedback widget like the one proposed in the last patch could be loaded conditionally.

dmitrig01’s picture

Concerns:
- can't we put an #element_validate on the textarea as well for the length?
- what if I don't want my textarea to have this? For most text areas I don't want this.

floretan’s picture

Status: Needs review » Needs work

@dmitrig01, the point of this patch was to have the browser limit the input length on textareas like it does for textfields when "maxlength" is used, but I agree that the server-side validation should perform the same check.

Even with this patch applied, textareas don't have this character limitation widget unless they have the FAPI #maxlength property set.

Changing status to "code needs work", because even if the widget is good the approach to where and how it is applied needs to be revised (see chx's comment on #20).

sun’s picture

Status: Needs work » Closed (won't fix)

I'm sorry.

Tinkered a bit about this, but it won't fix.

1) Input filters. What you have in your plain-text textarea may be expanded into _a lot_ more.

2) Text formats. Those can be switched on the client-side, at run-time. So this would only work for form elements without #text_format, i.e. plain textareas.

3) Client-side editors aka. WYSIWYG. Those (usually) replace the textarea. Short-circuit to 2).

So, if at all, this feature could be applied to plain form elements of #type 'textarea' without #text_format.

But, you know what? This would be a great contrib module! :) [but please check whether one doesn't exist already]

giorgio79’s picture