jQuery Max Length

A jQuery plugin that applies a maximum length to a textarea.

The current version is 1.1.0 and is available under the GPL and MIT licences. For more detail see the documentation reference page. Or see a minimal page that you could use as a basis for your own investigations.

Default Settings

The max length functionality can easily be added to a textarea with appropriate default settings.
You can also remove the max length functionality if it is no longer required, or disable or enable the field to receive input.

Default max length:

 

$('#defaultLength').maxlength();

$('#removeLength').toggle(function() {
		$(this).text('Re-attach');
		$('#defaultLength').maxlength('destroy');
	},
	function() {
		$(this).text('Remove');
		$('#defaultLength').maxlength();
	});

$('#disableLength').toggle(function() {
		$(this).text('Enable');
		$('#defaultLength').maxlength('disable');
	},
	function() {
		$(this).text('Disable');
		$('#defaultLength').maxlength('enable');
	});

You can override the defaults globally as shown below:

$.maxlength.setDefaults({showFeedback: true});

Processed fields are marked with a class of hasMaxLength and are not re-processed if targetted a second time.

Options

Customise the max length functionality through additional settings.

Different length:

$('#otherLength').maxlength({max: 300});

Don't truncate text:

$('#truncateLength').maxlength({truncate: false});

No feedback shown:

$('#noShowLength').maxlength({showFeedback: false});

Custom feedback:

$('#feedbackLength').maxlength({feedbackText: 'Used {c} of {m}'});

Feedback only when active:

$('#activeLength').maxlength({showFeedback: 'active'});

Feedback in another element:  

$('#targetLength').maxlength({
	showFeedback: true, feedbackTarget: '#targetFeedback'});

Events

You can be notified when the textarea has filled via the onFull setting.

When full:

$('#whenFullLength').maxlength({truncate: false,
	onFull: function(overflow) {
		if (!overflow) { // Notify via an alert
			alert('The field is full');
		}
		else { // Flash styling
			var self = $(this);
			self.removeClass('maxlength-overflow');
			setTimeout(function() {
					self.addClass('maxlength-overflow');
				}, 250);
		}
	}});

Styling

You can adjust the appearance of the textarea and feedback via CSS. The default is to place the feedback immediately after the textarea with a slightly reduced font size.

The textarea is marked with the hasMaxLength class once initialised and any feedback element is marked with the maxlength-feedback class.

Both the textarea and its feedback are marked with the maxlength-full class when the textarea is full, and also with the maxlength-overflow class when even more text is entered.

Default style:

$('#styleLength').maxlength();

Style used here:

$('#hereLength').maxlength();
.maxlength-feedback { display: inline-block;
	width: 7em; margin: 0em 1em; vertical-align: top; }

Feedback below:

$('#belowLength').maxlength();
#belowLength + .maxlength-feedback { display: block;
	width: auto; margin-left: 20em; }

Overlaid feedback:

$('#overlaidLength').maxlength({feedbackText: '{c}/{m}'});
#overlaidLength { padding-top: 0.5em; }
#overlaidLength + .maxlength-feedback { position: relative;
	left: -5.25em; top: 1px; width: 4em; padding-right: 0.25em;
	color: #fff; background-color: #3c8243; text-align: right; }

In the Wild

This tab highlights examples of this plugin in use "in the wild".

To add another example, please contact me (kbwood{at}iinet.com.au) and provide the plugin name, the URL of your site, its title, and a short description of its purpose and where/how the plugin is used.

Quick Reference

A full list of all possible settings is shown below. Note that not all would apply in all cases. For more detail see the documentation reference page.

$(selector).maxlength({
	max: 200, // Maximum length
	truncate: true, // True to disallow further input, false to highlight only
	showFeedback: true, // True to always show user feedback, 'active' for hover/focus only
	feedbackTarget: null, // jQuery selector or function for element to fill with feedback
	feedbackText: '{r} characters remaining ({m} maximum)',
		// Display text for feedback message, use {r} for remaining characters,
		// {c} for characters entered, {m} for maximum
	overflowText: '{o} characters too many ({m} maximum)',
		// Display text when past maximum, use substitutions above
		// and {o} for characters past maximum
	onFull: null // Callback when full or overflowing,
		// receives one parameter: true if overflowing, false if not
});

$.maxlength.setDefaults(settings) // Change settings for all instances

$(selector).maxlength('option', settings) // Change the instance settings
$(selector).maxlength('option', name, value) // Change an instance setting
$(selector).maxlength('option', name) // Retrieve an instance setting

$(selector).maxlength('enable') // Enable the max length functionality
$(selector).maxlength('disable') // Disable the max length functionality

$(selector).maxlength('destroy') // Remove the max length functionality

Usage

  1. Include the jQuery library in the head section of your page.
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  2. Download and include the jQuery Max Length CSS and JavaScript in the head section of your page.
    <style type="text/css">@import "jquery.maxlength.css";</style>
    <script type="text/javascript" src="jquery.maxlength.js"></script>
    Alternately, you can use the minimised version jquery.maxlength.min.js (5.5K vs 10.9K, 1.7K when zipped).
  3. Connect the max length functionality to your textareas.
    $(selector).maxlength();
    You can include custom settings as part of this process.
    $(selector).maxlength({max: 300});

For more detail see the documentation reference page. Or see a minimal page that you could use as a basis for your own investigations.

Comments

None as yet.

Contact Keith Wood at kbwood{at}iinet.com.au with comments or suggestions.

Change History

VersionDateChanges
1.1.004 Aug 2012
  • Added onFull callback
  • Amalgamated 'change' and 'settings' commands into 'option' command
  • Added 'enable', 'disable', and 'curLength' commands
  • Updated underlying plugin framework
1.0.316 Jul 2011
  • Made counting of CR/LF consistent
  • Set alternate feedback correctly
  • Return cursor to bottom of textarea when truncating
1.0.212 Mar 2011
  • Added truncate and overflowText settings
  • Added 'active' option for showFeedback
  • Added feedbackTarget setting
  • Corrected handling of special characters when full
1.0.115 May 2010
  • Added settings command
1.0.006 Feb 2010
  • Initial release

Valid HTML 4.01 Strict