Skip to content

Commit e0d4d19

Browse files
Field Progress Bar Counter
1 parent b1b1b19 commit e0d4d19

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function onLoad() {
2+
var fieldName = 'short_description'; // Change to your field name
3+
var maxLength = 160;
4+
var warningThreshold = 0.7;
5+
var criticalThreshold = 0.9;
6+
7+
var fieldElement = g_form.getControl(fieldName);
8+
if (!fieldElement) return;
9+
10+
// Add progress bar initially
11+
addProgressBar(fieldElement, fieldElement.value);
12+
13+
// Attach keyup event for real-time updates
14+
fieldElement.addEventListener('keyup', function() {
15+
addProgressBar(fieldElement, fieldElement.value);
16+
});
17+
}
18+
19+
function addProgressBar(fieldElement, value) {
20+
var maxLength = 160;
21+
var warningThreshold = 0.7;
22+
var criticalThreshold = 0.9;
23+
24+
// Remove any existing progress bar
25+
var existingBar = document.getElementById(fieldElement.name + '_progress');
26+
if (existingBar) {
27+
existingBar.parentNode.removeChild(existingBar);
28+
}
29+
30+
// Create progress bar container
31+
var container = document.createElement('div');
32+
container.id = fieldElement.name + '_progress';
33+
container.style.cssText = 'width: 100%; height: 4px; background: #e0e0e0; margin-top: 4px; border-radius: 2px; transition: all 0.3s ease;';
34+
35+
// Create progress bar fill
36+
var fill = document.createElement('div');
37+
fill.style.cssText = 'height: 100%; width: 0%; border-radius: 2px; transition: all 0.3s ease;';
38+
39+
// Calculate percentage
40+
var percent = (value.length / maxLength) * 100;
41+
percent = Math.min(percent, 100);
42+
43+
// Set fill width and color
44+
fill.style.width = percent + '%';
45+
if (percent >= criticalThreshold * 100) {
46+
fill.style.backgroundColor = '#ff4444';
47+
} else if (percent >= warningThreshold * 100) {
48+
fill.style.backgroundColor = '#ffbb33';
49+
} else {
50+
fill.style.backgroundColor = '#00C851';
51+
}
52+
53+
// Create percentage label
54+
var label = document.createElement('div');
55+
label.style.cssText = 'font-size: 11px; color: #666; margin-top: 2px; text-align: right;';
56+
label.textContent = Math.round(percent) + '% used';
57+
58+
// Assemble and insert the progress bar
59+
container.appendChild(fill);
60+
container.appendChild(label);
61+
62+
// Insert after the field element
63+
var parent = fieldElement.parentNode;
64+
parent.insertBefore(container, fieldElement.nextSibling);
65+
66+
// Add warning message if over limit
67+
if (value.length > maxLength) {
68+
g_form.addErrorMessage('This field exceeds the maximum length of ' + maxLength + ' characters');
69+
} else {
70+
g_form.clearMessages();
71+
}
72+
}

0 commit comments

Comments
 (0)