|
| 1 | +# Word Counter Display |
| 2 | + |
| 3 | +## Use Case |
| 4 | +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. |
| 5 | + |
| 6 | +## Requirements |
| 7 | +- ServiceNow instance |
| 8 | +- Client Script execution rights |
| 9 | +- Text fields requiring word count validation |
| 10 | + |
| 11 | +## Implementation |
| 12 | +1. Create a new Client Script with Type "onChange" |
| 13 | +2. Copy the script code from `script.js` |
| 14 | +3. Configure the field name and word limit in the script |
| 15 | +4. Apply to desired table/form |
| 16 | +5. Save and test |
| 17 | + |
| 18 | +## Configuration |
| 19 | +Edit these variables in the script: |
| 20 | + |
| 21 | +```javascript |
| 22 | +var fieldName = 'short_description'; // Your field name |
| 23 | +var maxWords = 25; // Your word limit |
| 24 | +``` |
| 25 | + |
| 26 | +## Features |
| 27 | +- Real-time word counting as user types |
| 28 | +- Visual indicators: info (blue), warning (yellow), error (red) |
| 29 | +- Shows "X out of Y words" format with status messages |
| 30 | +- Automatically trims whitespace and handles multiple spaces |
| 31 | +- Automatically clears previous messages |
| 32 | + |
| 33 | +## Common Examples |
| 34 | +```javascript |
| 35 | +// Short Description (25 words) |
| 36 | +var fieldName = 'short_description'; |
| 37 | +var maxWords = 25; |
| 38 | + |
| 39 | +// Description (500 words) |
| 40 | +var fieldName = 'description'; |
| 41 | +var maxWords = 500; |
| 42 | + |
| 43 | +// Work Notes (200 words) |
| 44 | +var fieldName = 'work_notes'; |
| 45 | +var maxWords = 200; |
| 46 | + |
| 47 | +// Comments (300 words) |
| 48 | +var fieldName = 'comments'; |
| 49 | +var maxWords = 300; |
| 50 | + |
| 51 | +// Close Notes (100 words) |
| 52 | +var fieldName = 'close_notes'; |
| 53 | +var maxWords = 100; |
| 54 | +``` |
| 55 | + |
| 56 | +## Message Thresholds |
| 57 | +- **10+ words remaining**: Info message (blue) |
| 58 | +- **1-5 words remaining**: Warning message (yellow) - "Approaching limit" |
| 59 | +- **Over limit**: Error message (red) - "Limit exceeded" |
| 60 | + |
| 61 | +## Notes |
| 62 | +- Uses standard ServiceNow APIs: `g_form.showFieldMsg()` and `g_form.hideFieldMsg()` |
| 63 | +- Create separate Client Scripts for multiple fields |
| 64 | +- Works with all text fields and text areas |
| 65 | +- Word count excludes extra whitespace and empty strings |
| 66 | +- Counts words by splitting text on space boundaries after trimming |
0 commit comments