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.
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | textarea_maxlenght-119388-19b.patch | 3.9 KB | floretan |
| #14 | textarea_maxlength.patch | 2.74 KB | ChrisKennedy |
| #11 | textarea_maxlength_0.patch | 2.85 KB | ChrisKennedy |
| #9 | textarea_maxlength.patch | 3.19 KB | ChrisKennedy |
| #4 | maxlength.js_.txt | 873 bytes | aries |
Comments
Comment #1
ardas commentedThis 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.
Comment #2
aries commentedMaybe this code: http://test2.danieleastwell.co.uk/test/textarea-max/textarea-max.html
Comment #3
aries commentedA better jQuery based solution: http://jquery.com/pipermail/discuss_jquery.com/2007-January/022409.html
Comment #4
aries commentedHere's my modificated maxlength (the original has made by Bjorn Wijers) jQuery code. Currently is only works for one textarea per page.
Comment #5
aries commentedComment #6
ChrisKennedy commentedThis 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.
Comment #7
ChrisKennedy commentedThis 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.
Comment #8
ChrisKennedy commentedHere is a basic implementation.
Comment #9
ChrisKennedy commentedPatch.
Comment #10
ChrisKennedy commentedComment #11
ChrisKennedy commentedRe-rolled and cleaned up. You can try it out by adding a maxlength to the comment textarea in comment_form().
Comment #12
jrabeemer commentedWe need an interface in CCK add textarea field to set the limit. :-p
Comment #13
catchNice idea but no longer applies, and will have to wait for D7.
Comment #14
ChrisKennedy commentedHere is a re-roll.
Comment #15
ChrisKennedy commentedAnyone care to review?
Comment #16
ardas commentedI'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...
Comment #17
ardas commentedCCK textarea already has a maxlength field but it is ignored since there is no support of this feature in the textarea element.
Comment #18
pasquallesubscribe
Comment #19
floretan commentedWorks 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.
Comment #20
chx commentedThis 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.
Comment #21
floretan commentedI 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.
Comment #22
dmitrig01 commentedConcerns:
- 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.
Comment #23
floretan commented@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).
Comment #24
sunI'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]
Comment #25
giorgio79 commentedhttp://www.drupal.org/project/maxlength