diff --git a/Client-Side Components/Client Scripts/Whitespace Validation/Readme.md b/Client-Side Components/Client Scripts/Whitespace Validation/Readme.md new file mode 100644 index 0000000000..9e7dc7f7ce --- /dev/null +++ b/Client-Side Components/Client Scripts/Whitespace Validation/Readme.md @@ -0,0 +1,35 @@ +Whitespace Validation — Client Script (ServiceNow) +Overview + +This Client Script ensures that a field does not contain any whitespace characters. It validates user input in real-time and provides immediate feedback by displaying an inline error message directly below the field. Invalid input is cleared automatically to enforce proper data entry. + +Features + +Prevents any spaces in the field value. + +Displays field-level error messages below the input field. + +Automatically clears invalid input for user correction. + +Simple, lightweight, and does not use regex or getLabelOf. + +Works dynamically for any field where the Client Script is applied. + +Usage Instructions +1. Create the Client Script + +Navigate to System Definition → Client Scripts. + +Click New to create a new Client Script. + +2. Configure the Script + +Name: Whitespace Validation + +Table: Select the target table (e.g., sys_user) + +Type: onChange + +Field: Select the field to validate (e.g., username, phone) + +Active: Check this box diff --git a/Client-Side Components/Client Scripts/Whitespace Validation/White Space Validation.js b/Client-Side Components/Client Scripts/Whitespace Validation/White Space Validation.js new file mode 100644 index 0000000000..6bffc37b00 --- /dev/null +++ b/Client-Side Components/Client Scripts/Whitespace Validation/White Space Validation.js @@ -0,0 +1,24 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading || !newValue) return; + + var fieldName = control.name; + + // Check for any whitespace + if (newValue.includes(' ')) { + // Show inline error message below the field + if (g_form.showFieldMsg) { + g_form.showFieldMsg( + fieldName, + 'This field cannot contain spaces.', + 'error', + false // false = show below the field + ); + } + + // Clear the field + g_form.setValue(fieldName, ''); + } else { + // Remove any previous message if input is valid + if (g_form.hideFieldMsg) g_form.hideFieldMsg(fieldName, true); + } +}