To restrict special characters in a Single Line Textbox

25421
Tera Contributor

in Choice box -To restrict special characters in a Single Line Textbox?

 

3 REPLIES 3

J Siva
Tera Sage

Hi @25421 
You can use client scripts with Regex to validate user input. Please share more details about where you're planning to implement this validation.

Regards,
Siva

Anurag Tripathi
Mega Patron
Mega Patron

If you know what all characters to restrict, a simp[le way would be using a client script as below

var string = g_form.getValue('field_name');
if (string.indexOf("#") > -1 || string.indexOf("*") > -1) { //here its restricting # and *, tyou can add more
alert("Please do not use the # or * characters");
g_form.clearValue('field_name');
}

 

Below is a more elaborate solution using regex

Solved: Restriction validation for single line text field - ServiceNow Community

-Anurag

kaushal_snow
Mega Sage

Hi @25421 ,

 

You can create an onChange client script that triggers whenever the value of the Single Line Text field changes. 

 

var regex = /[!@#$%^&*(),.?":{}|<>]/g;
if (regex.test(newValue)) {
alert('Special characters are not allowed.');
g_form.setValue(control.name, oldValue); 

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/