Adding validation on decimal field to allow show only one digit after decimal

Sanel
Tera Expert

I have a decimal field on which i have to apply validation that it shows only one digit after the decimal point .

And also applying validation on String field that it shows only two characters.

How can I apply validation in servicenow?
Thank you for any help.

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi Sanel,

 

You can create 2 onChange client scripts that runs onChange() of decimal field & string field as below.

1. For decimal check.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var gr = g_form.getValue('new'); //replace decimal field name here
	
	var newis=parseFloat(gr).toFixed(1); //converts 12.3456 to 12.3
alert('Decimal field can have only 1 number after decimal');
	g_form.setValue('new',newis);	//replace new with decimal field name
}

 

2. For String field check.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var gr = g_form.getValue('new'); //replace new with string field name
		if(gr.length>2 || gr.length<2)
			{
		alert('String field can have only 2 characters maximum');
		g_form.setValue('new',''); //replace new with string field name
			}
}

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Sanel,

 

You can create 2 onChange client scripts that runs onChange() of decimal field & string field as below.

1. For decimal check.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var gr = g_form.getValue('new'); //replace decimal field name here
	
	var newis=parseFloat(gr).toFixed(1); //converts 12.3456 to 12.3
alert('Decimal field can have only 1 number after decimal');
	g_form.setValue('new',newis);	//replace new with decimal field name
}

 

2. For String field check.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var gr = g_form.getValue('new'); //replace new with string field name
		if(gr.length>2 || gr.length<2)
			{
		alert('String field can have only 2 characters maximum');
		g_form.setValue('new',''); //replace new with string field name
			}
}

Miguel A Garcia
Mega Guru

Hi Sanel,

 

Here's an article on the recommended practice to validate input data using Variable Validation with RegEx:

https://community.servicenow.com/community?id=community_blog&sys_id=ed0733a2dbe490104819fb24399619dc

 

I'm not sure if you want this for the Service Catalogue or for forms in the classic UI; there might be some differences in how to apply this, but in both cases I'd use RegEx and would look something like:

^[0-9]*(\.[0-9])?$

I drafted that TOOO fast and will probably have flaws, but you can check it with in following page:

https://regexr.com/

 

To make sure that it works when there's no decimal, when there is, not allowing two decimal positions, etc, etc...

 

Hope this helped. Kind regards,

Miguel A. Garcia

SN Technical Consultant at FlyForm Ltd

 

PS: you can also check the RegEx library for already made expressions to reuse:

http://regexlib.com/?AspxAutoDetectCookieSupport=1