Last active
October 2, 2015 19:28
-
-
Save wpscholar/2304179 to your computer and use it in GitHub Desktop.
A jQuery plugin that places a character limit on text inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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