jquery.inputlimiter.1.3.1.js
6.34 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* jQuery Input Limiter plugin 1.3.1
* http://rustyjeans.com/jquery-plugins/input-limiter/
*
* Copyright (c) 2009 Russel Fones <russel@rustyjeans.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
$.fn.inputlimiter = function (options) {
var opts = $.extend({}, $.fn.inputlimiter.defaults, options),
$elements = $(this);
if (opts.boxAttach && !$('#' + opts.boxId).length) {
$('<div/>').appendTo("body").attr({id: opts.boxId, 'class': opts.boxClass}).css({'position': 'absolute'}).hide();
// apply bgiframe if available
if ($.fn.bgiframe) {
$('#' + opts.boxId).bgiframe();
}
}
var inputlimiterKeyup = function (e) {
var $this = $(this),
count = counter($this.val());
if (!opts.allowExceed && count > opts.limit) {
$this.val(truncater($this.val()));
}
if (opts.boxAttach) {
$('#' + opts.boxId).css({
'width': $this.outerWidth() - ($('#' + opts.boxId).outerWidth() - $('#' + opts.boxId).width()) + 'px',
'left': $this.offset().left + 'px',
'top': ($this.offset().top + $this.outerHeight()) - 1 + 'px',
'z-index': 2000
});
}
var charsRemaining = (opts.limit - count > 0 ? opts.limit - count : 0),
remText = opts.remTextFilter(opts, charsRemaining),
limitText = opts.limitTextFilter(opts);
if (opts.limitTextShow) {
$('#' + opts.boxId).html(remText + ' ' + limitText);
// Check to see if the text is wrapping in the box
// If it is lets break it between the remaining test and the limit test
var textWidth = $("<span/>").appendTo("body").attr({id: '19cc9195583bfae1fad88e19d443be7a', 'class': opts.boxClass}).html(remText + ' ' + limitText).innerWidth();
$("#19cc9195583bfae1fad88e19d443be7a").remove();
if (textWidth > $('#' + opts.boxId).innerWidth()) {
$('#' + opts.boxId).html(remText + '<br />' + limitText);
}
// Show the limiter box
$('#' + opts.boxId).show();
} else {
$('#' + opts.boxId).html(remText).show();
}
},
inputlimiterKeypress = function (e) {
var count = counter($(this).val());
if (!opts.allowExceed && count > opts.limit) {
var modifierKeyPressed = e.ctrlKey || e.altKey || e.metaKey;
if (!modifierKeyPressed && (e.which >= 32 && e.which <= 122) && this.selectionStart === this.selectionEnd) {
return false;
}
}
},
inputlimiterBlur = function () {
var $this = $(this);
count = counter($this.val());
if (!opts.allowExceed && count > opts.limit) {
$this.val(truncater($this.val()));
}
if (opts.boxAttach) {
$('#' + opts.boxId).fadeOut('fast');
} else if (opts.remTextHideOnBlur) {
var limitText = opts.limitText;
limitText = limitText.replace(/\%n/g, opts.limit);
limitText = limitText.replace(/\%s/g, (opts.limit === 1 ? '' : 's'));
$('#' + opts.boxId).html(limitText);
}
},
counter = function (value) {
if (opts.limitBy.toLowerCase() === "words") {
return (value.length > 0 ? $.trim(value).replace(/\ +(?= )/g, '').split(' ').length : 0);
}
var count = value.length,
newlines = value.match(/\n/g);
if (newlines && opts.lineReturnCount > 1) {
count += newlines.length * (opts.lineReturnCount - 1);
}
return count;
},
truncater = function (value) {
if (opts.limitBy.toLowerCase() === "words") {
return $.trim(value).replace(/\ +(?= )/g, '').split(' ').splice(0, opts.limit).join(' ') + ' ';
}
return value.substring(0, opts.limit);
};
$(this).each(function (i) {
var $this = $(this);
if ((!options || !options.limit) && opts.useMaxlength && parseInt($this.attr('maxlength')) > 0 && parseInt($this.attr('maxlength')) != opts.limit) {
$this.inputlimiter($.extend({}, opts, { limit: parseInt($this.attr('maxlength')) }));
} else {
if (!opts.allowExceed && opts.useMaxlength && opts.limitBy.toLowerCase() === "characters") {
$this.attr('maxlength', opts.limit);
}
$this.unbind('.inputlimiter');
$this.bind('keyup.inputlimiter', inputlimiterKeyup);
$this.bind('keypress.inputlimiter', inputlimiterKeypress);
$this.bind('blur.inputlimiter', inputlimiterBlur);
}
});
};
$.fn.inputlimiter.remtextfilter = function (opts, charsRemaining) {
var remText = opts.remText;
if (charsRemaining === 0 && opts.remFullText !== null) {
remText = opts.remFullText;
}
remText = remText.replace(/\%n/g, charsRemaining);
remText = remText.replace(/\%s/g, (opts.zeroPlural ? (charsRemaining === 1 ? '' : 's') : (charsRemaining <= 1 ? '' : 's')));
return remText;
};
$.fn.inputlimiter.limittextfilter = function (opts) {
var limitText = opts.limitText;
limitText = limitText.replace(/\%n/g, opts.limit);
limitText = limitText.replace(/\%s/g, (opts.limit <= 1 ? '' : 's'));
return limitText;
};
$.fn.inputlimiter.defaults = {
limit: 255,
boxAttach: true,
boxId: 'limiterBox',
boxClass: 'limiterBox',
remText: '%n character%s remaining.',
remTextFilter: $.fn.inputlimiter.remtextfilter,
remTextHideOnBlur: true,
remFullText: null,
limitTextShow: true,
limitText: 'Field limited to %n character%s.',
limitTextFilter: $.fn.inputlimiter.limittextfilter,
zeroPlural: true,
allowExceed: false,
useMaxlength: true,
limitBy: 'characters',
lineReturnCount: 1
};
})(jQuery);