This is easy to do for "textfields" of course (by using the maxlength attribute), but I have not been able to figure out how to do this with "textareas" and cck yet since the maxlength attribute does not work for "textareas".

Any tips?

CommentFileSizeAuthor
#2 text.module_4.patch899 byteskarens

Comments

eliza411’s picture

I agree that this should be built-in, but until it is . . . here are some steps that look like they'll work. We did it a bit differently, putting maxlength into the attributes, but that's a non-standard value and your page won't validate. When I get a little more time, I'll probably change to the way they're doing it here.

http://drupal.org/node/80122

karens’s picture

Component: General » text.module
Status: Active » Needs review
StatusFileSize
new899 bytes

Here's a simple patch to get the text module to do the validation for you.

Caleb G2’s picture

Wow, thanks Karen!

yched’s picture

Just as a comment : it should probably be noted that this "max length" includes the html tags.
Meaning if your textarea uses TinyMCE (for instance), this limit is only "unpredictably" related to what the user actually "sees".

Maybe there could be an additional (I know...) checkbox to specify if the max length is intended with or without tags (and then use strip_tags PHP function).

karens’s picture

I guess I was assuming that this would be used by people who are not using html, since 'length' in an html document is really meaningless (do you count all the empty spaces that don't display in the finished html or somehow try to count only spaces that actually get displayed in the finished document, etc. etc.) Stripping the tags would only be a start to trying to figure out the length of the displayed text. If I was using html I would leave maxlenth to 0 and not validate it.

yched’s picture

Yes, I didn't think about whitespaces.
I think the relevant target here is what gets displayed in the final html output. Multiple spaces/tabs/linebreaks could be tackled with a rather simple preg_replace.
What are the other "etc, etc" you have in mind ?

karens’s picture

Not really sure, I haven't thought it through. Carriage returns, indents, blockquotes, etc. What is the length of indented text like blockquotes or ul or dl items, for instance? Do you count the bullet as a space? What else is there in html where the display is different than the source code? As I said, I haven't really thought it through other than to think it might be a lot more complicated than it would appear.

RobRoy’s picture

I'd say just include all characters/tags in the max length. It would be too difficult to determine what the char count of a WYSIWG field would be. Maybe throw something in the help text about that.

karens’s picture

Status: Needs review » Fixed

Committed this change (simple test for length of the field). No validation is done if length is set to zero, so you can use that if you have html code in the field since it appears that may be too complex to address. At any rate, if a value is set the module should certainly validate it, so this seems like a bug that needs to be fixed.

dkruglyak’s picture

Title: How to get CCK to validate for max length in textarea? » How to get CCK to validate for length in textarea?
Status: Fixed » Active

Could we add a feature to also validate for min length?

This is also a common requirement - to ensure that the input meets minimum content size standards.

yched’s picture

Status: Active » Fixed

http://drupal.org/node/97983 has been added for "min length" validation.

Anonymous’s picture

Status: Fixed » Closed (fixed)