Created
March 22, 2013 23:58
-
-
Save evadi/5225675 to your computer and use it in GitHub Desktop.
jQuery plugin for character limits in text areas
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
(function ($) { | |
$.fn.characterLimit = function (options) { | |
var settings = $.extend({ | |
'maxCharacters': 100 | |
}, options); | |
return this.each(function () { | |
var $this = $(this); | |
$this.keypress(function (e) { | |
var char = String.fromCharCode(e.which); | |
var curVal = $(this).val(); | |
var forecastVal = curVal + char; | |
//when the character limit is about to be exceeded preventDefault | |
//halts the new character being assigned to the text area | |
if (forecastVal.length >= settings.maxCharacters) { | |
e.preventDefault(); | |
} | |
}); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment