-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.character-limit.js
47 lines (38 loc) · 1.47 KB
/
jquery.character-limit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//==================================================================
// Plugin written by David Ferguson
// http://code.davidferguson.me.uk
//
// version: 0.1.0
// date: 22/03/2013
//
// Anyone can use/modify/copy the source on any system, for any use,
// without express permission from the author
//==================================================================
(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();
}
});
$this.bind("paste", function (e) {
//allows the paste operation to finish so the text is ready to inspect
var source = $(this);
setTimeout(function () {
var val = source.val();
source.val(val.substring(0, settings.maxCharacters));
}, 0);
});
});
};
})(jQuery);