Skip to content

Commit 24c770f

Browse files
Update Count letters.js
1 parent 0e7b04e commit 24c770f

File tree

1 file changed

+34
-1
lines changed
  • Client-Side Components/Catalog Client Script/Real time count of letters

1 file changed

+34
-1
lines changed
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
1-
test
1+
(function executeRule(current, g_form, g_user, g_scratchpad) {
2+
var fieldName = 'short_description'; // Change to your field name
3+
var maxChars = 500;
4+
5+
// Create a span element for character count
6+
var charCounterId = 'char-counter-' + fieldName;
7+
if (!document.getElementById(charCounterId)) {
8+
var counterSpan = document.createElement('span');
9+
counterSpan.id = charCounterId;
10+
counterSpan.style.fontSize = '12px';
11+
counterSpan.style.color = '#555';
12+
var fieldElement = g_form.getControl(fieldName);
13+
fieldElement.parentNode.appendChild(counterSpan);
14+
}
15+
16+
// Update character count on keyup
17+
var fieldElement = g_form.getControl(fieldName);
18+
fieldElement.addEventListener('keyup', function () {
19+
var currentLength = fieldElement.value.length;
20+
var counterSpan = document.getElementById(charCounterId);
21+
counterSpan.textContent = currentLength + ' / ' + maxChars + ' characters';
22+
23+
// Show warning if limit exceeded
24+
if (currentLength > maxChars) {
25+
counterSpan.style.color = 'red';
26+
g_form.addErrorMessage('Character limit exceeded! Please shorten your text.');
27+
g_form.setSubmit(false); // Block submit
28+
} else {
29+
counterSpan.style.color = '#555';
30+
g_form.clearMessages();
31+
g_form.setSubmit(true);
32+
}
33+
});
34+
})(current, g_form, g_user, g_scratchpad);

0 commit comments

Comments
 (0)