Skip to content

Commit 823ef12

Browse files
Create Remaining Field Character Counter.js
This script provides immediate feedback to users by displaying the remaining characters for a text field, which is especially useful for fields with a maximum length
1 parent 97b5e29 commit 823ef12

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2+
if (isLoading || newValue === '') {
3+
return;
4+
}
5+
var fieldName = 'name';
6+
7+
var maxLength = g_form.getControl(fieldName).getAttribute('maxlength');
8+
if (maxLength) {
9+
10+
var remaining = maxLength - newValue.length;
11+
var message = 'Remaining characters: ' + remaining;
12+
13+
// Clear previous messages to avoid duplication.
14+
g_form.hideFieldMsg(fieldName);
15+
16+
if (remaining <= 10 && remaining >= 0) {
17+
g_form.showFieldMsg(fieldName, message, 'warning');
18+
} else if (remaining < 0) {
19+
g_form.showFieldMsg(fieldName, 'Maximum characters exceeded!', 'error');
20+
} else {
21+
g_form.showFieldMsg(fieldName, message, 'info');
22+
}
23+
}
24+
25+
26+
27+
}

0 commit comments

Comments
 (0)