BLOG: Mitch Wilson, Austin TX Web & Multimedia Developer – Austin, TX

maxChar jQuery plugin

Add a powerful character counter to your Web forms. Highly customizable with many options, maxChar rocks. And it's free. And it's a jQuery plugin. And people love it. Check out the comments on version 0.1.0.

Example character counter

This example uses the optional setting called "label" that displays when the form field is not in focus. When the user starts typing, the label is dynamically replaced with the character count.

Example file missing. You browser does not support iframes. Sorry.

<script type="text/javascript" src="jquery.maxchar.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Pass HTML element id and character limit.
$('#message').maxChar(50, {label: 'Type message'});
});
</script>

Versions

We're on the third release, 0.3.0. See more at this blog post maxChar jQuery Plugin: New Release 0.3.

Download from the maxChar jQuery plugin page.

Bug Fixes

For information about the bugs fixed, see the maxChar bugs page.

Version 0.3.0 Parameters

A jQuery plugin allows configuration via the settings object. maxChar offers several optional settings passed as parameters.

  • debug - Specify whether to send message updates to the Firebug console. Default is false.
  • indicator - Specify alternate indicator element by id. Default is indicator created dynamically.
  • label - Specify a default label displayed when input element is not in focus. Default is blank.
  • pluralMessage - Set the plural form of the remaining count message. Default is ' remaining'.
  • rate - Set the update rate in milliseconds. Default is 200.
  • singularMessage - Set the singular form of the remaining count message. Default is ' remaining'.
  • spaceBeforeMessage - Set spacing in front of (to the left of) the indicator message. Default is ' '.
  • truncate - Truncate submitted value down to limit on form submit. Default is true.

Instructions

  1. Add links to jQuery and the maxChar plugin in your HTML head section.


  2. In your HTML head section, initialize the plugin in your jQuery ready block.


  3. In your HTML, just make sure your input or textarea element has the id you passed to maxChar above.

    <form>
    <textarea id="message"></textarea>
    </form>

    NOTE: although below the only element you create is the textarea element, the maxChar plugin dynamically creates and adds a span element after the textarea element. This dynamically created span element is the container for the updated status message, eg, "N remaining."

Examples

See the parameters list above for all settings. These example show a couple of optional settings but not all of them.

Debug parameter example

Specify whether to send message updates to the Firebug console. Default is false.


<script type="text/javascript">
$(document).ready(function(){
// Pass HTML element id and character limit.
$('#message').maxChar(50, {debug:true});
});
</script>

Indicator parameter example

By default, if you do not specify an indicator, maxChar creates one for you dynamically. If you do specify an indicator, maxChar will use it instead.


<script type="text/javascript">
$(document).ready(function(){
// Pass HTML element id and character limit.
$('#message').maxChar(50, {indicator:'custom_element'});
});
</script>

Then in your HTML , you can create an element to use as custom indicator for displaying update messages


<div id="custom_element"></div>

Multiple parameters

Pass multiple parameters, in this case the singularMessage and pluralMessage parameters


<script type="text/javascript">
$(document).ready(function(){
// Pass HTML element id and character limit.
$('#message').maxChar(50, {singularMessage:' character remaining',pluralMessage:' characters remaining'});
});
</script>

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • RSS
  • Twitter
Comments (4) Trackbacks (2)
  1. Make sure to note the new feature, label, added in the last version, 0.2.0. By specifying a label, you can add a label next to the field that is dynamically replaced by the character counter when the user clicks on the field.

  2. Is it possible to dynamically add a class to the span containing the counter (id=characterCounter) when the counter reaches zero or less?

    Even though it shows a negative value it’s very easy for the user to miss that they have typed over the limit. It would be helpful to then set a class that for example sets the negative number to red. The class would obviously have to be removed if the user deletes text and the counter goes above zero.

    Thanks for a useful plugin!

  3. Updated email address.

  4. @charlie,

    Absolutely. In fact, I had it in there at one point as part of validation. Then I removed validation to focus the plugin more on it’s core object: providing a character counter.

    I am definitely going to re-add it and will try to do so in the next couple of days. I have not had a lot of time lately to provide support for this plugin, which is still in beta. I’m very glad for your interest and I will have more time soon.


Leave a comment