Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Client-Side Components/Client Scripts/field word counter/README.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions Client-Side Components/Client Scripts/field word counter/script.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
Loading