Skip to content

Commit c925318

Browse files
diptopolDiptopol Dam
authored andcommitted
Fixed text count for new line
If stopInputAtMaximum is set true and we try to insert more character than max limit, plugin will trim the text. For twoCharCarriageReturn option true, if we add a newline which exceeds max limit, plugin will trim but total text count do not decrease by 2. Fixed the issue by reusing textCount for trimmed text.
1 parent 810c3a6 commit c925318

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

textcounter.js

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,7 @@
4646
eventTriggered = e.originalEvent === undefined ? false : true;
4747

4848
if (!$.isEmptyObject($text)) {
49-
if (base.options.type == "word") { // word count
50-
textCount = $text.trim().replace(/\s+/gi, ' ').split(' ').length;
51-
}
52-
else { // character count
53-
// count carriage returns/newlines as 2 characters
54-
if (base.options.twoCharCarriageReturn) {
55-
var carriageReturns = $text.match(/(\r\n|\n|\r)/g),
56-
carriageReturnsCount = 0;
57-
58-
if (carriageReturns !== null) {
59-
carriageReturnsCount = carriageReturns.length;
60-
}
61-
}
62-
63-
if (base.options.countSpaces) { // if need to count spaces
64-
textCount = $text.replace(/[^\S\n|\r|\r\n]/g, ' ').length;
65-
}
66-
else {
67-
textCount = $text.replace(/\s/g, '').length;
68-
}
69-
70-
// count extended characters (e.g. Chinese)
71-
if (base.options.countExtendedCharacters) {
72-
var extended = $text.match(/[^\x00-\xff]/gi);
73-
74-
if (extended == null) {
75-
textCount = $text.length;
76-
} else {
77-
textCount = $text.length + extended.length;
78-
}
79-
}
80-
81-
if (base.options.twoCharCarriageReturn) {
82-
textCount += carriageReturnsCount;
83-
}
84-
}
49+
textCount = base.textCount($text);
8550
}
8651

8752
// if max is auto retrieve value
@@ -159,7 +124,8 @@
159124

160125
$this.val(trimmedString.trim());
161126

162-
textTotalCount = base.options.countDown ? 0 : base.options.max;
127+
textCount = base.textCount($this.val());
128+
textTotalCount = base.options.countDown ? base.options.max - textCount : textCount;
163129
base.setCount(textTotalCount);
164130
} else {
165131
base.setErrors('max');
@@ -173,6 +139,61 @@
173139
}
174140
};
175141

142+
base.textCount = function(text) {
143+
var textCount = 0;
144+
145+
if (base.options.type == "word") { // word count
146+
textCount = base.wordCount(text);
147+
}
148+
else { // character count
149+
textCount = base.characterCount(text);
150+
}
151+
152+
return textCount;
153+
};
154+
155+
base.wordCount = function(text) {
156+
return text.trim().replace(/\s+/gi, ' ').split(' ').length;
157+
};
158+
159+
base.characterCount = function(text) {
160+
var textCount = 0,
161+
carriageReturnsCount = 0;
162+
163+
// count carriage returns/newlines as 2 characters
164+
if (base.options.twoCharCarriageReturn) {
165+
var carriageReturns = text.match(/(\r\n|\n|\r)/g);
166+
167+
if (carriageReturns !== null) {
168+
carriageReturnsCount = carriageReturns.length;
169+
}
170+
}
171+
172+
if (base.options.countSpaces) { // if need to count spaces
173+
textCount = text.replace(/[^\S\n|\r|\r\n]/g, ' ').length;
174+
}
175+
else {
176+
textCount = text.replace(/\s/g, '').length;
177+
}
178+
179+
// count extended characters (e.g. Chinese)
180+
if (base.options.countExtendedCharacters) {
181+
var extended = text.match(/[^\x00-\xff]/gi);
182+
183+
if (extended == null) {
184+
textCount = text.length;
185+
} else {
186+
textCount = text.length + extended.length;
187+
}
188+
}
189+
190+
if (base.options.twoCharCarriageReturn) {
191+
textCount += carriageReturnsCount;
192+
}
193+
194+
return textCount;
195+
};
196+
176197
base.setCount = function(count) {
177198
base.$text_counter.text(count);
178199
};

textcounter.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)