Okay, so I want to add a maxlength to a textarea, however, form api doesn't appear to have a way to do this. It only has #maxlength for textfields. I have a form for my users to enter data, my php breaks it up and sends the information into a table in my database...problem is, if a user enters too much data at once, it can completely slow down my entire site and sometimes almost make it so slow it basically crashes. I need to limit how much my users enter in at once. Ideally I want to add a maxlength and some how communicate to the user that he/she has gone over the limit...I was thinking about a counter, but not sure how I'd do that with form api. Anyways, for now I'm just looking to add a limit. How can I do this?
Thanks,
Collin
Comments
removed
removed
Interactive help - javascript to the rescue
Definitely do the validation hook stuff. For those who want more interactive feedback for the user, this trick works for javascript-enabled browsers (in addition to the validation code shown elsewhere in the comments.)
'#attributes'=>array('onKeyPress'=>"return(this.value.length<$max_length);")(Add that to your form field definition, as in the example below)
-------------
Michael Curry
Amadorable - RubyPowered - RoadCarvin'
Thanks for directing me to
Thanks for directing me to the solution. I found the big problem in this script - all navigation buttons stop working when you reach maximum. You can't even refresh page using F5 button.
Here is the revised solution. Set onKeyPress event for your textarea like this:
Create a JS file and add it using drupal_add_js:
Inside myfile.js define the following function:
This was tested in FireFox, Opera and IE 6.
----------------
Regards,
Dmitry Kresin, ARDAS group (ARDAS group)
Why don't you try to
Why don't you try to implement this? http://test2.danieleastwell.co.uk/test/textarea-max/textarea-max.html
--
Fehér János aka Aries
http://aries.mindworks.hu
This might come in handy...
... thanks for sharing.
_validate
You can use _validate, in which you can count characters, words, ... For example, if your form looks like this:
you could do:
or for chars:
Validation isn't that
Validation isn't that solution clients want to see. May be because it is slow - you need to refresh your page.
----------------
Regards,
Dmitry Kresin, ARDAS group (ARDAS group)
Maxlength
Have you tried the Maxlength module?
Large Robot
http://www.largerobot.com
Pretty JS method
You can do it with JS. Just preventing user to add more characters on keyboard, and show him gray background (like disabled area)
In your module:
In JS (Don't pay attention to php tagss, added for highlihting on d.org)
Easiest way
OMG, why not this?
Works like a charm, thanks
Works like a charm, thanks men ;)
Using the devel module [for
Using the devel module [for example, using the dpm command in form_alter hook - "dpm($form)"], drill down into the $form array and you'll see where to adjust the value to set 'maxlength'.
"$form['my_textarea_form_field']['und'][0]['value']['#attributes']['maxlength'] = 1000;" worked for me. You can adjust this value in a form_alter hook, or anywhere else you have access to the form array before it is sent to the theme engine...I think. For a counter, I would be using an ajax callback. But I know there are many ways to skin drupal cats.
Works for me! thanks :D
Works for me! thanks :D
Drupal 7 max length character count
Download and enable https://www.drupal.org/project/maxlength, then you simply can use
#maxlength_js:Gots Damn Brilliant, Leymannx!
Brillant! Thank you for that suggestion -- saved me some troubles.
Worked out perfectly! Thanks again!!
a non-module solution if using EntityForms
If using EntityForms, you could use plain old javascript in a "static" field right after the textareas are defined:
in drupal context ...
Why are you necroposting to a decade on thread?
He's building a Drupal website ....
How exactly is he going to inject the javascript in the context of a DRupal website building without the use of yet another module?
spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...
Easy there fella
Right oh mate, relax.
Comment above relates specifically EntityForms. Instead of adding another module, use entityform's static field to inject some JS.