5

I would like to add a simple character counter on a form textarea.

Is there a module that does this?

I don't want to limit the number of characters that can be input; I only want to display how many characters have been input in the textarea so far.

2
  • 1
    @PatrickKenny If you're happy using the method in this comment you can still use MaxLength. It would just need a form alter on the private message form, and set $form['message']['#maxlength_js'] = TRUE;, and optionally $form['message']['#maxlength_js_truncate_html'] = TRUE if the message field uses a WYSIWYG (as well as setting #maxlength on the element too). I haven't tested it but looks like it should work
    – Clive
    Aug 16, 2013 at 18:44
  • @Clive Thanks, that did the trick. I added the following code to hook_form_privatemsg_new_alter(): $form['body']['#maxlength'] = 100000; $form['body']['#maxlength_js'] = TRUE; $form['body']['#attributes']['maxlength_js_label'] = t("Messages must be at least 200 characters. Number of characters so far: @count"); Aug 19, 2013 at 17:23

2 Answers 2

6
+100

The Maxlength module has that functionality baked right in:

Maxlength allows you to set maximum length of any field on any form making use of the form API. This includes fields created using the UI. (Example: any text field on a node)

When the maximum length is enforced for a field, while editing content of that type some Java Script will countdown how many characters are left, also if the Javascript is turned off, it falls back to form API validation.

It works great for textfields and textareas alike. This is (roughly) what a textarea looks like after Maxlength is applied:

enter image description here

7
  • Ok, but I don't have to limit charachters, however I try :)
    – arrubiu
    Jul 17, 2012 at 8:48
  • 1
    You can set a ridiculously high limit (1000000 or something) and use the field settings to change the text that's displayed underneath...you can use that to get just a character count, without the countdown
    – Clive
    Jul 17, 2012 at 8:58
  • this a solution but I've many fields. Maxlenght is not the solution and there aren't other modules
    – arrubiu
    Jul 17, 2012 at 9:01
  • 3
    It's worth noting that simple use cases don't always equal simple solutions. If you have a simple text field, with no line breaks or html or anything it could be fairly simple, but as soon as you have a text area or allow html ar anything like that things get more complex. Since the maxlength module has already tacked a lot of these issues, if you were to make your own you could make a new custom module by changing the name of the maxlength module, then modifying it to remove the max length part, so you just have the count part. That might be the easiest solution.
    – rooby
    Aug 16, 2013 at 15:13
  • 2
    Another solution could be the module Ckeditor, that add the chars and words count, but I've to add the ckeditor toolbar to textarea interested.
    – arrubiu
    Aug 18, 2013 at 15:54
0

Checkout this simple jquery solution: https://stackoverflow.com/a/5371160/947370

You could make a tiny module, put the jQuery directly in your theme, or use https://drupal.org/project/js_injector to add it just to the pages where you need it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.