From 724744defb93034fc2bb324f017d461a1de4fb96 Mon Sep 17 00:00:00 2001 From: Rajat Date: Sat, 4 Oct 2025 11:53:21 +0530 Subject: [PATCH 1/3] add client script: word counter in a field --- .../field word counter/README.md | 66 +++++++++++++++++++ .../field word counter/script.js | 22 +++++++ 2 files changed, 88 insertions(+) create mode 100644 Client-Side Components/Client Scripts/field word counter/README.md create mode 100644 Client-Side Components/Client Scripts/field word counter/script.js diff --git a/Client-Side Components/Client Scripts/field word counter/README.md b/Client-Side Components/Client Scripts/field word counter/README.md new file mode 100644 index 0000000000..a9cadbbb91 --- /dev/null +++ b/Client-Side Components/Client Scripts/field word counter/README.md @@ -0,0 +1,66 @@ +# Word Counter Display + +## Use Case +Provides real-time word count feedback for text fields in ServiceNow forms. Shows current word count against maximum limit in "X out of Y words" format with visual indicators to help users stay within word limits. + +## Requirements +- ServiceNow instance +- Client Script execution rights +- Text fields requiring word count validation + +## Implementation +1. Create a new Client Script with Type "onChange" +2. Copy the script code from `script.js` +3. Configure the field name and word limit in the script +4. Apply to desired table/form +5. Save and test + +## Configuration +Edit these variables in the script: + +```javascript +var fieldName = 'short_description'; // Your field name +var maxWords = 25; // Your word limit +``` + +## Features +- Real-time word counting as user types +- Visual indicators: info (blue), warning (yellow), error (red) +- Shows "X out of Y words" format with status messages +- Automatically trims whitespace and handles multiple spaces +- Automatically clears previous messages + +## Common Examples +```javascript +// Short Description (25 words) +var fieldName = 'short_description'; +var maxWords = 25; + +// Description (500 words) +var fieldName = 'description'; +var maxWords = 500; + +// Work Notes (200 words) +var fieldName = 'work_notes'; +var maxWords = 200; + +// Comments (300 words) +var fieldName = 'comments'; +var maxWords = 300; + +// Close Notes (100 words) +var fieldName = 'close_notes'; +var maxWords = 100; +``` + +## Message Thresholds +- **10+ words remaining**: Info message (blue) +- **1-5 words remaining**: Warning message (yellow) - "Approaching limit" +- **Over limit**: Error message (red) - "Limit exceeded" + +## Notes +- Uses standard ServiceNow APIs: `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` +- Create separate Client Scripts for multiple fields +- Works with all text fields and text areas +- Word count excludes extra whitespace and empty strings +- Counts words by splitting text on space boundaries after trimming \ No newline at end of file diff --git a/Client-Side Components/Client Scripts/field word counter/script.js b/Client-Side Components/Client Scripts/field word counter/script.js new file mode 100644 index 0000000000..1a23185ee3 --- /dev/null +++ b/Client-Side Components/Client Scripts/field word counter/script.js @@ -0,0 +1,22 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading) return; + + // USER CONFIGURATION: Set field name and word limit + var fieldName = 'short_description'; // Change to your field name + var maxWords = 25; // Change to your word limit + + var trimmedValue = newValue ? newValue.trim() : ''; + var currentWords = trimmedValue ? trimmedValue.replace(/\s+/g, ' ').split(' ').length : 0; + + // Clear any existing messages + g_form.hideFieldMsg(fieldName); + + // Show appropriate message based on word count + if (currentWords > maxWords) { + g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error'); + } else if (currentWords >= maxWords - 5) { + g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning'); + } else { + g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words', ' info'); + } +} \ No newline at end of file From 45cbfe1797b41efc03aa270fd04dff06c7900dad Mon Sep 17 00:00:00 2001 From: Rajat Date: Sat, 4 Oct 2025 13:43:32 +0530 Subject: [PATCH 2/3] changed function from showFieldMsg to addInfoMessage --- .../Client Scripts/field word counter/README.md | 2 +- .../Client Scripts/field word counter/script.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Client-Side Components/Client Scripts/field word counter/README.md b/Client-Side Components/Client Scripts/field word counter/README.md index a9cadbbb91..418d3682fb 100644 --- a/Client-Side Components/Client Scripts/field word counter/README.md +++ b/Client-Side Components/Client Scripts/field word counter/README.md @@ -59,7 +59,7 @@ var maxWords = 100; - **Over limit**: Error message (red) - "Limit exceeded" ## Notes -- Uses standard ServiceNow APIs: `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` +- Uses standard ServiceNow APIs: `g_form.addInfoMessage()` and `g_form.hideFieldMsg()` - Create separate Client Scripts for multiple fields - Works with all text fields and text areas - Word count excludes extra whitespace and empty strings diff --git a/Client-Side Components/Client Scripts/field word counter/script.js b/Client-Side Components/Client Scripts/field word counter/script.js index 1a23185ee3..cf537f6be3 100644 --- a/Client-Side Components/Client Scripts/field word counter/script.js +++ b/Client-Side Components/Client Scripts/field word counter/script.js @@ -13,10 +13,10 @@ function onChange(control, oldValue, newValue, isLoading) { // Show appropriate message based on word count if (currentWords > maxWords) { - g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error'); + g_form.addErrorMessage(fieldName, currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error'); } else if (currentWords >= maxWords - 5) { - g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning'); + g_form.addInfoMessage(fieldName, currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning'); } else { - g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words', ' info'); + g_form.addInfoMessage(fieldName, currentWords + ' out of ' + maxWords + ' words', ' info'); } } \ No newline at end of file From 5446345744b4adce36ec0939d6a3ce9edc55807c Mon Sep 17 00:00:00 2001 From: Rajat Date: Sat, 4 Oct 2025 14:03:13 +0530 Subject: [PATCH 3/3] changed hideFieldMsg to clearMessages --- .../Client Scripts/field word counter/script.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Client-Side Components/Client Scripts/field word counter/script.js b/Client-Side Components/Client Scripts/field word counter/script.js index cf537f6be3..8346ce6f70 100644 --- a/Client-Side Components/Client Scripts/field word counter/script.js +++ b/Client-Side Components/Client Scripts/field word counter/script.js @@ -1,22 +1,22 @@ function onChange(control, oldValue, newValue, isLoading) { if (isLoading) return; - + // USER CONFIGURATION: Set field name and word limit var fieldName = 'short_description'; // Change to your field name var maxWords = 25; // Change to your word limit - + var trimmedValue = newValue ? newValue.trim() : ''; var currentWords = trimmedValue ? trimmedValue.replace(/\s+/g, ' ').split(' ').length : 0; - + // Clear any existing messages - g_form.hideFieldMsg(fieldName); - + g_form.clearMessages(); + // Show appropriate message based on word count if (currentWords > maxWords) { - g_form.addErrorMessage(fieldName, currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error'); + g_form.addErrorMessage(fieldName + ' ' + currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error'); } else if (currentWords >= maxWords - 5) { - g_form.addInfoMessage(fieldName, currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning'); + g_form.addInfoMessage(fieldName + ' ' + currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning'); } else { - g_form.addInfoMessage(fieldName, currentWords + ' out of ' + maxWords + ' words', ' info'); + g_form.addInfoMessage(fieldName + ' ' + currentWords + ' out of ' + maxWords + ' words', ' info'); } } \ No newline at end of file