From 7490d8dc50379819c0563c1712948e3431e519ca Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Tue, 21 Oct 2025 13:09:11 +0530 Subject: [PATCH 1/2] Create Readme.md --- .../Readme.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Client-Side Components/Client Scripts/Only number validation for input/Readme.md diff --git a/Client-Side Components/Client Scripts/Only number validation for input/Readme.md b/Client-Side Components/Client Scripts/Only number validation for input/Readme.md new file mode 100644 index 0000000000..950916c0d2 --- /dev/null +++ b/Client-Side Components/Client Scripts/Only number validation for input/Readme.md @@ -0,0 +1,44 @@ +Numeric-Only Field Validation — Client Script (ServiceNow) +Overview + +This Client Script validates that a specific field contains numbers only on ServiceNow forms. +If the user enters non-numeric characters, it: + +Resets the field to the previous valid value. + +Displays an inline error message directly below the field. + +Ensures previous error messages are cleared to avoid duplicates. + +This script works in Classic UI forms and safely handles environments where certain g_form APIs may not be available. + +Features + +Validates input dynamically as the user types (OnChange). + +Resets invalid input to previous value. + +Shows a clear, field-level error message: "Please enter numbers only". + +Avoids displaying messages at the top of the form. + +Compatible with instances where g_form.showFieldMsg, g_form.hideFieldMsg, or g_form.setValue may not exist. + +Usage Instructions +1. Create the Client Script + +Navigate to System Definition → Client Scripts. + +Click New to create a client script. + +2. Configure the Script + +Name: Numeric-Only Validation + +Table: Choose the table you want to validate (e.g., incident). + +Type: onChange + +Field: The field you want to validate (e.g., short_description). + +Active: Checked From 02191536882db0b1aab33de05ed596881e1c899e Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Tue, 21 Oct 2025 13:10:11 +0530 Subject: [PATCH 2/2] Create Number Validation Script.js --- .../Number Validation Script.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Client-Side Components/Client Scripts/Only number validation for input/Number Validation Script.js diff --git a/Client-Side Components/Client Scripts/Only number validation for input/Number Validation Script.js b/Client-Side Components/Client Scripts/Only number validation for input/Number Validation Script.js new file mode 100644 index 0000000000..6ab3d57255 --- /dev/null +++ b/Client-Side Components/Client Scripts/Only number validation for input/Number Validation Script.js @@ -0,0 +1,30 @@ +function onChange(control, oldValue, newValue, isLoading, g_form) { + if (isLoading || newValue === '') return; + + var fieldName = control.name; + var fieldLabel = g_form.getLabel ? g_form.getLabel(fieldName) : fieldName; + var numericPattern = /^[0-9]+$/; + + // Hide old messages safely + if (g_form.hideFieldMsg) g_form.hideFieldMsg(fieldName, true); + + // Validation logic + if (!numericPattern.test(newValue)) { + // Reset only if API supported + if (g_form.setValue && typeof g_form.setValue === 'function') { + g_form.setValue(fieldName, oldValue); + } + + // Show the message directly below the field + if (g_form.showFieldMsg) { + g_form.showFieldMsg( + fieldName, + 'Please enter numbers only', // your custom message + 'error', + false // false = display under the field, not at top + ); + } else { + alert('Short Description filed accepts numbers only'); + } + } +}