Skip to content

Commit 57630c2

Browse files
Field Progress Bar Counter
1 parent b1b1b19 commit 57630c2

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Field Progress Bar Counter
2+
3+
## Overview
4+
This client script adds a visual progress bar indicator when users type in text fields, showing how much of the field's maximum length has been used. It provides a more intuitive way to track character limits compared to simple numeric counters.
5+
6+
## Features
7+
- Visual progress bar that updates in real-time
8+
- Color-coded feedback (green, yellow, red) based on usage
9+
- Percentage indicator alongside the progress bar
10+
- Smooth transitions between states
11+
- Works with any text field with a character limit
12+
13+
## Requirements
14+
- ServiceNow instance
15+
- Client Script execution rights
16+
- Text fields with character limits (e.g., short_description, description)
17+
18+
## Implementation Steps
19+
1. Navigate to System Definition → Client Scripts
20+
2. Create a new Client Script with these settings:
21+
- Table: Choose your target table (e.g., incident, sc_req_item)
22+
- Type: onChange
23+
- Field name: Your target field (e.g., short_description)
24+
3. Copy the code from script.js into your client script
25+
4. Configure the maxLength variable if needed
26+
5. Save and test
27+
28+
## Configuration
29+
The script can be customized by modifying these variables:
30+
```javascript
31+
var maxLength = 160; // Maximum characters allowed
32+
var warningThreshold = 0.7; // Show yellow at 70% capacity
33+
var criticalThreshold = 0.9; // Show red at 90% capacity
34+
```
35+
36+
## How It Works
37+
1. The script creates a progress bar div element below the field
38+
2. As the user types, it calculates the percentage of used characters
39+
3. The progress bar fills proportionally to character usage
40+
4. Colors change based on defined thresholds:
41+
- Green: Normal usage
42+
- Yellow: Approaching limit
43+
- Red: Near/at limit
44+
45+
## Benefits
46+
- Improved user experience with visual feedback
47+
- Reduces likelihood of hitting character limits unexpectedly
48+
- Helps users self-regulate content length
49+
- Modern, professional appearance
50+
- Zero server calls - all client-side
51+
52+
## Usage Example
53+
When implementing on an Incident's short description:
54+
```javascript
55+
function onChange(control, oldValue, newValue, isLoading) {
56+
if (isLoading || newValue === oldValue) {
57+
return;
58+
}
59+
showProgressBar(control, newValue);
60+
}
61+
```
62+
63+
## Compatibility
64+
- Works in all modern browsers
65+
- Compatible with both classic and next-experience UIs
66+
- Responsive design adapts to field width
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || newValue === oldValue) {
3+
return;
4+
}
5+
6+
showProgressBar(control, newValue);
7+
}
8+
9+
function showProgressBar(control, value) {
10+
var maxLength = 160; // Configurable maximum length
11+
var warningThreshold = 0.7; // Show yellow at 70% capacity
12+
var criticalThreshold = 0.9; // Show red at 90% capacity
13+
14+
// Remove any existing progress bar
15+
var existingBar = gel(control.name + '_progress');
16+
if (existingBar) {
17+
existingBar.parentNode.removeChild(existingBar);
18+
}
19+
20+
// Create progress bar container
21+
var container = document.createElement('div');
22+
container.id = control.name + '_progress';
23+
container.style.cssText = 'width: 100%; height: 4px; background: #e0e0e0; margin-top: 4px; border-radius: 2px; transition: all 0.3s ease;';
24+
25+
// Create progress bar fill
26+
var fill = document.createElement('div');
27+
fill.style.cssText = 'height: 100%; width: 0%; border-radius: 2px; transition: all 0.3s ease;';
28+
29+
// Calculate percentage
30+
var percent = (value.length / maxLength) * 100;
31+
percent = Math.min(percent, 100); // Cap at 100%
32+
33+
// Set fill width and color
34+
fill.style.width = percent + '%';
35+
if (percent >= criticalThreshold * 100) {
36+
fill.style.backgroundColor = '#ff4444'; // Red
37+
} else if (percent >= warningThreshold * 100) {
38+
fill.style.backgroundColor = '#ffbb33'; // Yellow
39+
} else {
40+
fill.style.backgroundColor = '#00C851'; // Green
41+
}
42+
43+
// Create percentage label
44+
var label = document.createElement('div');
45+
label.style.cssText = 'font-size: 11px; color: #666; margin-top: 2px; text-align: right;';
46+
label.textContent = Math.round(percent) + '% used';
47+
48+
// Assemble and insert the progress bar
49+
container.appendChild(fill);
50+
container.appendChild(label);
51+
52+
// Insert after the control
53+
var parent = control.getElement().parentNode;
54+
parent.insertBefore(container, control.getElement().nextSibling);
55+
56+
// Add warning message if over limit
57+
if (value.length > maxLength) {
58+
g_form.addErrorMessage('This field exceeds the maximum length of ' + maxLength + ' characters');
59+
} else {
60+
g_form.clearMessages();
61+
}
62+
}

0 commit comments

Comments
 (0)