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,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');
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Loading