Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The script enhances a form field (specifically the description field) by:
-Adding a live word counter below the field.
-Visually warning the user if the word count exceeds 150 words.

This client-side script, intended for use in a ServiceNow form (e.g., catalog item or incident form), dynamically appends a custom `<div>` element below the `description` field to display a real-time word count. It leverages the `g_form.getControl()` API to access the field's DOM element and attaches an `input` event listener to monitor user input. The script calculates the word count by splitting the input text using a regular expression (`\s+`) and updates the counter accordingly. It applies conditional styling to the counter (`green` if ≤150 words, `red` if >150), providing immediate visual feedback to the user to enforce input constraints.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function onLoad() {
var field = g_form.getControl('description');
var counter = document.createElement('div');
counter.id = 'desc_word_counter';
counter.style.marginTop = '5px';
field.parentNode.appendChild(counter);

field.addEventListener('input', function() {
var wordCount = field.value.trim().split(/\s+/).length;
counter.innerText = 'Word Count: ' + (field.value ? wordCount : 0);
if (wordCount > 150) {
counter.style.color = 'red';
} else {
counter.style.color = 'green';
}
});
}
Loading