-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextareaCounter.js
55 lines (51 loc) · 1.79 KB
/
TextareaCounter.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
48
49
50
51
52
53
54
55
(function ($) {
$.fn.TextareaCounter = function (options) {
var opts = $.extend($.fn.TextareaCounter.defaults, options);
return this.each(function () {
var $this = $(this),
$status = $this.parent().find(opts.statusSelector),
$statusCount = $status.find(opts.statusCountSelector),
truncate = $this.parent().find('[data-truncate]'),
maxLen = $this.attr(opts.maxLengthAttribute),
countFn = opts.countFn,
lastVal = $this.val();
$this.bind('keyup change', function () {
var left = maxLen - countFn($this.val());
if (0 > left) {
if(truncate.length) {
if($status.hasClass('counterWords')) {
lastVal = $this.val().split(" ").splice(0,maxLen).join(" ");
} else {
lastVal = $this.val().substr(0, maxLen);
}
}
if (!$this.hasClass('ui-state-error')) {
$this.val(lastVal);
$status.addClass('ui-state-error');
$statusCount.text('0');
}
} else {
lastVal = $this.val();
$statusCount.text(left);
if (0 < left) {
$status.removeClass('ui-state-error');
}
}
});
});
};
$.fn.TextareaCounter.defaults = {
'statusSelector': 'span.counterChars',
'statusCountSelector': 'span',
'maxLengthAttribute': 'data-maxchars',
'countFn': function (val) {return val.trim().replace(/\r?\n/g, '\r\n').length; }
};
$(document).ready(function () {
$('textarea[data-maxchars]').TextareaCounter();
$('textarea[data-maxwords]').TextareaCounter({
'statusSelector': 'span.counterWords',
'maxLengthAttribute': 'data-maxwords',
'countFn': function (val) {return val.trim().split(/\s+/).length; }
});
});
}(jQuery));