A jQueryplugin
that applies a maximum length to a textarea.
The current version is 2.0.1 and is available
under the MIT licence.
For more detail see the documentation reference page.
Or see a minimal page that you could
use as a basis for your own investigations.
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.
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.
This tab highlights examples of this plugin in use "in the wild".
None as yet.
To add another example, please contact me (wood.keith{at}optusnet.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({
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
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
onFull: null // Callback when full or overflowing,
// receives one parameter: true if overflowing, false if not
});
$.maxlength.regionalOptions[] // Language/country-specific localisations
$.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
Include the jQuery library (1.7+) in the head section of your page.