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..418d3682fb --- /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.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 +- 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..8346ce6f70 --- /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.clearMessages(); + + // Show appropriate message based on word count + if (currentWords > maxWords) { + 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'); + } else { + g_form.addInfoMessage(fieldName + ' ' + currentWords + ' out of ' + maxWords + ' words', ' info'); + } +} \ No newline at end of file