Skip to content

Commit a1f42f4

Browse files
Validate Fields in Client Side (#2565)
* Create fieldValidation.js * Create README.md * README.md
1 parent 6c917c2 commit a1f42f4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
An `onLoad` client script that validates required fields in specific ServiceNow form views.
2+
3+
This ServiceNow client script provides automatic validation of required form fields when users access specific form views. The script runs immediately when a form loads and checks that critical fields are populated, displaying user-friendly error messages for any missing required information. This ensures data completeness and improves form submission success rates by catching validation issues early in the user workflow.
4+
5+
What This Script Does:
6+
The onLoad client script performs comprehensive field validation with these key capabilities:
7+
View-Specific Validation: Only triggers validation when accessing a designated form view
8+
Multiple Field Support: Validates multiple required fields simultaneously in a single operation
9+
Smart Field Detection: Uses field labels (not technical names) in error messages for better user experience
10+
Consolidated Error Display: Shows all missing required fields in a single, clear error message
11+
Immediate Feedback: Provides instant validation results as soon as the form loads
12+
Non-Intrusive Design: Displays informational errors without blocking form interaction
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function onLoad(){
2+
var targetViewName = 'your_target_view_name';
3+
var requiredFields = ['field1', 'field2', 'field3'];
4+
5+
var currentViewName = g_form.getViewName();
6+
7+
if (currentViewName === targetViewName) {
8+
var emptyFields = [];
9+
10+
for (var i = 0; i < requiredFields.length; i++) {
11+
var fieldValue = g_form.getValue(requiredFields[i]);
12+
if (!fieldValue || fieldValue.trim() === '') {
13+
emptyFields.push(g_form.getLabelOf(requiredFields[i]));
14+
}
15+
}
16+
17+
if (emptyFields.length > 0) {
18+
var errorMessage = "The following required fields cannot be empty: " +
19+
emptyFields.join(', ');
20+
g_form.addErrorMessage(errorMessage);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)