jQuery Max Length

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

The current version is 1.0.0 and is available under the GPL and MIT licences. For more detail see the documentation reference page.

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.

Default max length:   

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

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

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});

No feedback shown:

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

Custom feedback:

$('#feedbackLength').maxlength({feedbackText: 'Used {c} of {m}'});
#feedbackLength + .maxlength-feedback { clear: both; margin-left: 20em; }

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({
	length: 200, // Maximum length
	showFeedback: true, // True to show user feedback
	feedbackText: '{r} characters remaining ({m} maximum)'
		// Display text for feedback message, use {r} for remaining characters,
		// {c} for characters entered, {m} for maximum
});

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

$(selector).maxlength('change', settings) // Change the instance settings

$(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://code.jquery.com/jquery-latest.pack.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 packed version jquery.maxlength.pack.js (??K vs ??K), or the minified version jquery.maxlength.min.js (??K, ??K 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.

Comments

None as yet.

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

Change History

VersionDateChanges
1.0.0?? ??? 2009
  • Initial release