Skip to content

Commit b5f7669

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

File tree

1 file changed

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

1 file changed

+23
-31
lines changed
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
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);
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading) {
3+
return;
144
}
155

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';
6+
var maxChars = 100;//count of charaters
7+
var currentLength = newValue.length;
8+
9+
// Clear previous messages
10+
g_form.clearMessages();
11+
12+
// Show info message
13+
g_form.addInfoMessage('Character count: ' + currentLength + ' / ' + maxChars);
2214

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);
15+
if (currentLength > maxChars) {
16+
// Show error message
17+
g_form.addErrorMessage('Character limit exceeded! Please shorten your text.');
18+
g_form.showFieldMsg('short_description', 'Too many characters!', 'error');
19+
20+
// Make field mandatory to block submission
21+
g_form.setMandatory('short_description', true);
22+
} else {
23+
// Remove mandatory if valid
24+
g_form.setMandatory('short_description', false);
25+
}
26+
}

0 commit comments

Comments
 (0)