Skip to content

Instantly share code, notes, and snippets.

@kdimatteo
Last active August 29, 2015 13:57
Show Gist options
  • Save kdimatteo/9414367 to your computer and use it in GitHub Desktop.
Save kdimatteo/9414367 to your computer and use it in GitHub Desktop.
Warn if an input's length is close to it's max
// bind the listener and config via data-attributes
// <textarea data-maxwarn="true" data-maxchars="20" data-warnclass='too-many-characters'></textarea>
$(document).on('keyup', '[data-maxwarn=true]', function(e){
var tf = $(e.target);
var max = tf.data('maxchars');
if(tf.val().length > max - 10){
tf.data('hitmax', 'true');
tf.addClass(tf.data('warnclass'));
} else if(tf.data('hitmax') === 'true' && tf.val().length < max - 10){
tf.removeClass(tf.data('warnclass'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment