Last active
August 29, 2015 13:57
-
-
Save kdimatteo/9414367 to your computer and use it in GitHub Desktop.
Warn if an input's length is close to it's max
This file contains 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
// 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