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.

Catalog variable to accept decimal values

sushma9
Tera Contributor

 Hi ,

I have variable called Cost and it should accept the  both decimal values and number. How can i achieve this.

 

Example :

1.Cost : 100 

2. cost : 1000.12345

 Note : It should accept both numbers and decimal values 

2 ACCEPTED SOLUTIONS

Prithvi_b
Tera Guru

Hi @sushma9 

function onSubmit() {
var cs = g_form.getValue('cost');
var letter = /^[0-9.]+$/;
if ((cs.match(letter))) {
var value = Math.round(cs*100)/100; // 10 defines 1 decimals, 100 for 2, 1000 for 3
//alert(value);
g_form.setValue('cost',value); //Please set value according to your need
} else {
// g_form.addErrorMessage('Add only numerics');
g_form.showErrorBox('cost','Add only numerics');
g_form.submitted = false;
return false;

}

}

 

 

Please modify the code for two decimal positions. Please find the attached screenshot.

Instead of var value = Math.round(cs*1000)/1000; , i gave var value = Math.round(cs*100)/100; .

 

Thankyou,

Prithvi

View solution in original post

Hi @sushma9 ,

PFA.

If you find my reply as correct,  mark as Correct.

Thankyou

 

View solution in original post

9 REPLIES 9

sushma9
Tera Contributor

 

 

 

 

I am using  below code but allowing me the numbers but i need to allow the decimals as well update 4 digits

 

var regexp = /^[+]?\d*$/;

if (!regexp.test(newValue)) {

alert('Please enter numeric value');

g_form.setValue('v_sec_review_unknown', '');
g_form.clearValue('u_confirmed_cost_of_training_excluding_taxes');

}

 

 

 

sushma9
Tera Contributor

Hi All,

 i am able to  add the decimal values but i need to make them round of 3 digits . Can any one help me on this .

 ex: 100.12345 then it should be come as 100.123 or 100.124

 

var regexp = /^[0-9.]*$/;

if (!regexp.test(newValue)) {

alert('Please enter numeric value');

sushma9
Tera Contributor

Hi All,

 Can any one help me on this ,

 Thanks in Advance

Hi @sushma9 ,

 

I have created an Onsubmit catalog client client script. Please try this.

 

 

 

function onSubmit() {
var cs = g_form.getValue('cost');
var letter = /^[0-9.]+$/;
if ((cs.match(letter))) {
var value = Math.round(cs*1000)/1000; // 10 defines 1 decimals, 100 for 2, 1000 for 3
//alert(value);
g_form.setValue('cost',value); //Please set value according to your need
} else {
// g_form.addErrorMessage('Add only numerics');
g_form.showErrorBox('cost','Add only numerics');
g_form.submitted = false;
return false;

}

}

 

Thankyou,

prithvi