Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading