-
-
Save jpsirois/4731345 to your computer and use it in GitHub Desktop.
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
$(document).on("keypress", 'textarea[maxlength]', function(event){ | |
var ignore = [8,9,13,33,34,35,36,37,38,39,40,46], | |
$this = $(this), | |
maxlength = $this.attr('maxlength'), | |
code = $.data(this, 'keycode') | |
// check if maxlength has a value. | |
// The value must be greater than 0 | |
if (maxlength && maxlength > 0) { | |
// continue with this keystroke if maxlength | |
// not reached or one of the ignored keys were pressed. | |
return ( $this.val().length < maxlength | |
|| $.inArray(code, ignore) !== -1 ) | |
} | |
}) | |
$(document).on("paste", 'textarea[maxlength]', function() { | |
var $this = $(this), | |
maxlength = $this.attr('maxlength') | |
setTimeout(function () { | |
$this.val($this.val().substr(0,maxlength)) | |
}, 100) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Return False will completely disable the textarea box, you cannot interact with it , That's will force the user to refresh the page if he want to edit the box for changes .