Skip to content

Commit abbe375

Browse files
authored
added client script which shows count of characters remaining (#1723)
1 parent 14aea1d commit abbe375

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Field Character Counter
2+
3+
## Use Case
4+
Provides real-time character count feedback for text fields in ServiceNow forms. Shows remaining characters with visual indicators to help users stay within field limits.
5+
6+
## Requirements
7+
- ServiceNow instance
8+
- Client Script execution rights
9+
- Text fields with character limits
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 character 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 maxLength = 160; // Your character limit
24+
```
25+
26+
## Features
27+
- Real-time character counting as user types
28+
- Visual indicators: info (blue), warning (yellow), error (red)
29+
- Shows "X characters remaining" or "Exceeds limit by X characters"
30+
- Automatically clears previous messages
31+
32+
## Common Examples
33+
```javascript
34+
// Short Description (160 chars)
35+
var fieldName = 'short_description';
36+
var maxLength = 160;
37+
38+
// Description (4000 chars)
39+
var fieldName = 'description';
40+
var maxLength = 4000;
41+
42+
// Work Notes (4000 chars)
43+
var fieldName = 'work_notes';
44+
var maxLength = 4000;
45+
```
46+
47+
## Message Thresholds
48+
- **50+ remaining**: Info message (blue)
49+
- **1-20 remaining**: Warning message (yellow)
50+
- **Over limit**: Error message (red)
51+
52+
## Notes
53+
- Uses standard ServiceNow APIs: `g_form.showFieldMsg()` and `g_form.hideFieldMsg()`
54+
- Create separate Client Scripts for multiple fields
55+
- Works with all text fields and text areas
56+
- Character count includes all characters (spaces, punctuation, etc.)
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 character limit
5+
var fieldName = 'Description'; // Change to your field name
6+
var maxLength = 80; // Change to your character limit
7+
8+
var currentLength = newValue ? newValue.length : 0;
9+
var remaining = maxLength - currentLength;
10+
11+
// Clear any existing messages
12+
g_form.hideFieldMsg(fieldName);
13+
14+
// Show appropriate message based on remaining characters
15+
if (remaining < 0) {
16+
g_form.showFieldMsg(fieldName, 'Exceeds limit by ' + Math.abs(remaining) + ' characters', 'error');
17+
} else if (remaining <= 20) {
18+
g_form.showFieldMsg(fieldName, remaining + ' characters remaining', 'warning');
19+
} else if (remaining <= 50) {
20+
g_form.showFieldMsg(fieldName, remaining + ' characters remaining', 'info');
21+
}
22+
}

0 commit comments

Comments
 (0)