Skip to content

Instantly share code, notes, and snippets.

@jpsirois
Forked from dewyze/textarea-maxlength.js
Last active December 12, 2015 06:48
Show Gist options
  • Save jpsirois/4731345 to your computer and use it in GitHub Desktop.
Save jpsirois/4731345 to your computer and use it in GitHub Desktop.
$(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)
})
@Johntaa
Copy link

Johntaa commented Nov 10, 2013

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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment