Created
October 5, 2015 21:04
-
-
Save rbrenton/e780c1c5e69233efa46f to your computer and use it in GitHub Desktop.
jQuery to enforce maxlength for textarea elements.
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
// jQuery to enforce maxlength for textarea elements. | |
(function(){ | |
var check = function(e) { | |
var max = $(e).attr('maxlength'); | |
if (e.value.length > max) { | |
e.value = e.value.substr(0, max); | |
} | |
}; | |
$('body').on('keydown', 'textarea[maxlength]', function(){ check(this); }); | |
$('body').on('keyup', 'textarea[maxlength]', function(){ check(this); }); | |
$('body').on('change', 'textarea[maxlength]', function(){ check(this); }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment