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.

String field should not allow special characters

vijay39
Giga Expert

Hi Guys,

I created a catalog item with few variables. In that i have a User name variable (of type String).

It should allow only letters but not special characters like @ # % ^ etc..

How can i do this?

 

Regards,

Vijay

1 ACCEPTED SOLUTION

Nitesh Balusu
Giga Guru
function onChange(control, oldValue, newValue, isLoading, isTemplate) {


   if (isLoading || newValue === '') {


   return;


   }


   var regex = new RegExp("^[a-zA-Z0-9]*$");


   if(!regex.test(newValue)){


   alert('Incorrect username');


   g_form.setValue('u_user_name','');



   }


}

View solution in original post

3 REPLIES 3

SanjivMeher
Mega Patron
Mega Patron

Below link should help

 

https://www.codexworld.com/how-to/check-special-characters-using-javascript/


Please mark this response as correct or helpful if it assisted you with your question.

Nitesh Balusu
Giga Guru
function onChange(control, oldValue, newValue, isLoading, isTemplate) {


   if (isLoading || newValue === '') {


   return;


   }


   var regex = new RegExp("^[a-zA-Z0-9]*$");


   if(!regex.test(newValue)){


   alert('Incorrect username');


   g_form.setValue('u_user_name','');



   }


}

Jacklin
Kilo Explorer

 Hi,

I am new to Servicenow. I want to create a client script on Change. I want to validate whether the User ID starts with "THDM". How can i restrict those string using script. Can someone help?