The Zurich release has arrived! Interested in new features and functionalities? Click here for more

onChange Validation Script against Catalog Item Variable - HELP!

jbradley234
Kilo Explorer

Hi there,

I'm fairly new at using ServiceNow and I have a little bit of experience with JavaScript but I'm shaky with the ServiceNow API.

I am trying to put together a Service Catalog client script that validates on a variable (single line text field) so that users are only able to input numeric characters in this particular input.

I've tried a number of different scripts but I've not had much luck - after speaking to someone at ServiceNow they suggested that I look at some of the Validation Scripts in ServiceNow (System Definition > Validation Scripts). I had a look at the numeric script and tried this in my Service Catalog client script but it didn't work. After reviewing the code and carrying out some more research I can see that these scripts aren't using the GlideForm API and I believe this is where it is going wrong.

Has anybody else used Service Catalog Client Scripts to cover a similar scenario and if so please can you shine some light as to how you did it?

Many thanks 🙂
Jamie

5 REPLIES 5

DubeyN
Mega Expert

You can write the onChange client script on the field.Copy below code :

****************************************************************
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '')
{
return;
}
if (newValue.match(/^\d+$/))
{
return true;
}
else
{
alert('Please enter only numeric value');
}
}
****************************************************************

Thanks,
ND


Hi there,

Thanks for this, I've tested the script and it works like a dream!

Once again, can't thank you enough!

Jamie


Sorry to resurrect this old thread, but this is exactly what I'm needing too.   Where do I paste in the script though? I see no place to put javascript for this "single line text" variable.



**UPDATE**
Okay, I found that I needed to add a new "Catalog Client Script", and paste that code there.   However, is there any way to make this an "onkeyup" event ?   Perhaps onCellEdit?



I try the onCellEdit, and a simple alert() won't even pop up:



Screen Shot 05-26-15 at 03.53 PM.JPG


Usually you want an "onChange" script.