Skip to content

Commit 1471161

Browse files
committed
added mandatory field highlighter
1 parent 1b8364f commit 1471161

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Mandatory Field Highlighter
2+
3+
## Use Case / Requirement
4+
Visually highlight mandatory fields that are empty by adding a red border, making it easier for users to identify which required fields need to be completed.
5+
6+
## Solution
7+
An onLoad client script that:
8+
- Identifies all mandatory fields on the form
9+
- Adds red border styling to empty mandatory fields
10+
- Updates styling when fields are filled
11+
- Provides visual feedback to improve user experience
12+
13+
## Implementation
14+
Add this as an **onLoad** client script on any form where you want to highlight mandatory fields.
15+
16+
## Notes
17+
- Only highlights empty mandatory fields
18+
- Removes highlighting when fields are filled
19+
- Uses CSS border styling for visual emphasis
20+
- Works with standard ServiceNow forms
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function onLoad() {
2+
// Highlight mandatory fields that are empty
3+
highlightMandatoryFields();
4+
5+
// Set up listeners to update highlighting when fields change
6+
setupFieldListeners();
7+
8+
function highlightMandatoryFields() {
9+
var allFields = g_form.getFieldNames();
10+
11+
for (var i = 0; i < allFields.length; i++) {
12+
var fieldName = allFields[i];
13+
14+
// Check if field is mandatory and visible
15+
if (g_form.isMandatory(fieldName) && g_form.isVisible(fieldName)) {
16+
var fieldValue = g_form.getValue(fieldName);
17+
var fieldElement = g_form.getElement(fieldName);
18+
19+
if (fieldElement) {
20+
if (!fieldValue || fieldValue === '') {
21+
// Add red border for empty mandatory fields
22+
fieldElement.style.border = '2px solid #ff4444';
23+
fieldElement.style.boxShadow = '0 0 5px rgba(255, 68, 68, 0.3)';
24+
} else {
25+
// Remove highlighting for filled mandatory fields
26+
fieldElement.style.border = '';
27+
fieldElement.style.boxShadow = '';
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
function setupFieldListeners() {
35+
var allFields = g_form.getFieldNames();
36+
37+
for (var i = 0; i < allFields.length; i++) {
38+
var fieldName = allFields[i];
39+
40+
if (g_form.isMandatory(fieldName)) {
41+
// Add change listener to update highlighting
42+
(function(field) {
43+
g_form.addElementChangeListener(field, function() {
44+
setTimeout(highlightMandatoryFields, 100);
45+
});
46+
})(fieldName);
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)