You are here

Cool module: Maxlength (Character limit for Drupal fields)

It's strange that this isn't even a standard feature in Drupal, because I've had to implement it in projects more times than I can count:

You're creating a new content type, and based on the design, you really want to limit a field or two to a certain number of characters.  You can, of course, trim the output on the display or via views, etc, but sometimes you don't want to just trim or cut off the field - you want the author to ensure it makes sense within that character limit by enforcing it on the input end rather than just cutting it off during display!

Enter the Maxlength contrib module.  Its implementation uses javascript and it integrates directly with the fields UI on your content types.

It is exceedingly easy to use!

Here is what a standard node edit form looks like before I set up the maxlength module (this example uses a 'Bio' content type):

As you can see, I have a very long body field.

To start limiting my field characters, I need to setup the maxlength module the following way:

  1. Download and install the module normally: https://drupal.org/project/maxlength
  2. There is no module-specific configuration
  3. Go to your content type's 'Manage fields' screen and hit 'Edit' next to the field you want to character-limit.


     

    1. Maxlength JS - enter in the maximum character length you want enforced for the field
    2. Force text truncate - check this box if you want the user's pasted or entered text in the editing interface itself to be truncated the minute they hit the character limit.  Otherwise, their text can grow past the character limit, though they won't be able to 'save' their entry and their character count will show them how many characters they are over
    3. Truncate html - if the field is an HTML field, rather than plain text, this option, when checked, ensures that the character count does not include html tags (and thus also prevents faulty html)
    4. Count down message - I usually leave the default here, but you can change the exact message that is shown under the field, letting the user know the character limit and how many characters they have left (they are negatives when they are over).
       
  4. Here is the same editing screen after I set up the maximum character count of 550 on the Body field of my Bio content type (just as above):

 

Wow - my field is waaaaay too long (5 whole paragraphs).

As you can see there is minimal setup and the field configuration options are few and intuitive.  Hope this was helpful and that you'll have no trouble using this module in your future projects.

Let me know if you have any questions or comments below.

Share

Comments

hi, I installed the said module and could not see the counter for it. Any ideas why?

Hi,

How to prevent typing when maxlength reach?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.