Skip to content

Commit 724744d

Browse files
committed
add client script: word counter in a field
1 parent 5da39f5 commit 724744d

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading) return;
3+
4+
// USER CONFIGURATION: Set field name and word limit
5+
var fieldName = 'short_description'; // Change to your field name
6+
var maxWords = 25; // Change to your word limit
7+
8+
var trimmedValue = newValue ? newValue.trim() : '';
9+
var currentWords = trimmedValue ? trimmedValue.replace(/\s+/g, ' ').split(' ').length : 0;
10+
11+
// Clear any existing messages
12+
g_form.hideFieldMsg(fieldName);
13+
14+
// Show appropriate message based on word count
15+
if (currentWords > maxWords) {
16+
g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Limit exceeded', ' error');
17+
} else if (currentWords >= maxWords - 5) {
18+
g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words - Approaching limit', ' warning');
19+
} else {
20+
g_form.showFieldMsg(fieldName, currentWords + ' out of ' + maxWords + ' words', ' info');
21+
}
22+
}

0 commit comments

Comments
 (0)