Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

sLimit special characters and spaces

Arka Banerjee
Mega Guru

Hi Team,

 

Suppose I have a single line text field on a ritm. How to limit special characters and spaces input on that so that it accepts only numbers and alphabets

8 REPLIES 8

Hi @Arka Banerjee 

 

Can you give the following a try-

 

^[a-zA-Z0-9]*$

 

Regards,

Amit

This just limits special characters, not spaces

Hi @Arka Banerjee 

 

Strange as the same is working in background script. 

 

AmitPandey_0-1713872245504.png

 

var pattern = /^[a-zA-Z0-9]*$/;
var testStrings = [
    "HelloWorld123", // Should match
    "12345", // Should match
    "Test String", // Should not match (contains a space)
    "Special!Char", // Should not match (contains a special character)
    "Another_Test" // Should not match (contains an underscore)
];

for (var i = 0; i < testStrings.length; i++) {
    var inputString = testStrings[i];
    var isMatch = pattern.test(inputString);
    gs.info("Input String: " + inputString + ", Matched: " + isMatch);
}

 

Please mark my answer helpful and correct.

 

Regards,

Amit

Community Alums
Not applicable

Hi @Arka Banerjee ,

An alternative is - you can use OnChange client script on Requested Item table for the particular field which you are looking at and apply this script.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

// Regular expression to match only numbers and alphabets
var regex = /^[A-Za-z0-9]+$/;

if (!newValue.match(regex)) {
alert("Please enter only numbers and alphabets.");
g_form.setValue(control.name, oldValue); // Reset the field value to the old value
}
}

Mark my Answer Helpful or Accept Solution if it resolves your issue.