A jQuery plugin that applies a maximum length to a textarea.
The current version is 1.0.3 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.
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.
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}'});
#feedbackLength + .maxlength-feedback { clear: both; margin-left: 20em; }
Feedback only when active:
$('#activeLength').maxlength({showFeedback: 'active'});
Feedback in another element:
$('#targetLength').maxlength({showFeedback: true, feedbackTarget: '#targetFeedback'});
This tab highlights examples of this plugin in use "in the wild".
None as yet.
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.
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
});
$.maxlength.setDefaults(settings) // Change settings for all instances
$(selector).maxlength('change', settings) // Change the instance settings
$(selector).maxlength('settings') // Retrieve the instance settings
$(selector).maxlength('destroy') // Remove the max length functionality
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><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 (3.2K vs 8.1K), or the minified version
jquery.maxlength.min.js (4.1K, 1.4K when zipped).$(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.
None as yet.
Contact Keith Wood at kbwood{at}iinet.com.au with comments or suggestions.
| Version | Date | Changes |
|---|---|---|
| 1.0.3 | 16 Jul 2011 |
|
| 1.0.2 | 12 Mar 2011 |
|
| 1.0.1 | 15 May 2010 |
|
| 1.0.0 | 06 Feb 2010 |
|