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.

Field Validation

Rick1
Giga Contributor

Hello,

Farley new to developing in ServiceNow, but I am tasked with creating a numeric input field that can only contain a value that is a multiple of the number 8; example would be, 8, 16, 24, 32,40…….

I know this would be a UI action, but how can I code to validate the input?

Thanks in advance for your guidance.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rick,

You should validate this via OnChange Client Script. Please refer below code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if (newValue % 8 != 0) { // If the value is not multiple of the number 8; 
		alert("PASS ALERT MESSAGE HERE");
		g_form.clearValue('PASS FIELD COLUMN NAME HERE');
    
}

   //Type appropriate comment here, and begin script below
   
}

 

 

https://docs.servicenow.com/bundle/orlando-application-development/page/script/client-scripts/concep...

 

-Pradeep Sharma

 

View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rick,

You should validate this via OnChange Client Script. Please refer below code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if (newValue % 8 != 0) { // If the value is not multiple of the number 8; 
		alert("PASS ALERT MESSAGE HERE");
		g_form.clearValue('PASS FIELD COLUMN NAME HERE');
    
}

   //Type appropriate comment here, and begin script below
   
}

 

 

https://docs.servicenow.com/bundle/orlando-application-development/page/script/client-scripts/concep...

 

-Pradeep Sharma

 

Inactive_Us1931
Giga Expert

Hi Rick,

1) You need to create the 'Onchange' client script, as below. in the field name below select your Field name.

Script : 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(newValue % 8 == 0){
return true;
}
else{
g_form.showErrorBox('u_integer','please add only multiple of 8',false);
return false;
}

}

 

Please Mark it correct/helpful, if it helps you.

Regards,

Sachin

 

Prasant Kumar 1
Kilo Sage

Hi,

You can follow the below steps:-

Step 1:- Create an onChange client script for the field to validate it.

Script:-

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(newValue % 8 == 0){
return true
}
else{

g_form.clearValue('<Integer field name>');
g_form.showErrorBox('<Integer filed name>','please add only multiple of 8',false);
return false;
}

}

 

If i was able to solve your query, please mark my answer correct and helpful.

Thanks & Regards

Prasant kumar sahu