Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active October 2, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpscholar/2304179 to your computer and use it in GitHub Desktop.
Save wpscholar/2304179 to your computer and use it in GitHub Desktop.
A jQuery plugin that places a character limit on text inputs
/**
* jQuery plugin that limits the number of characters that can be used in a text field.
*/
(function( $ ){
$.fn.limitInput = function( limit ) {
return this.each( function() {
$(this).keypress( function() {
if( $(this).val().length > limit ) {
$(this).val( $(this).val().substring(0, limit) );
}
});
});
};
})( jQuery );
// Example usage
jQuery(document).ready(function($) {
$('textarea').limitInput( 100 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment