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.

What is the type of variable by which we can input numerical value with float(33.67)

Chandra18
Mega Sage

Hi expert, 

I have created a "single line text" variable and under "type specification" tab, I have select validation regex as "number". But problem is it is not taking any flot value like 33.45, 6789.877 etc. and It is showing field error message " Not a number".

I have attached both screenshot, What is the type of variable by which we can input numerical value with float(33.67)?

Chandra18_0-1666119145180.png

Chandra18_1-1666119401531.png

Thanks

 

4 REPLIES 4

Jace Benson
Mega Sage

The number validation is just checking if you have a number 0-9, not for decimals.  You can create your own regex's to do things like this by navigating to;

  1. Service Catalog
    1. Catalog Variables
      1. Variable Validation Regex

Once there you can create one, I called mine "Floating point" with this regex; 

[+-]?([0-9]*[.])?[0-9]+

 This will let you have things like;

.123

4.56

5

Then once you've made that, you can pick on your variable.  

Hi @Jace Benson 
also, catalog client script not adding(sum) floating values.
Code is:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var value1 = g_form.getValue("p_amount");
var value2 = g_form.getValue("l_amount");
alert("value1: " + value1);
alert("value2: " + value2);

var sum = parseInt(value1) + parseInt(value2);
alert("sum: " + sum);
g_form.setValue('total_amount', sum);

}

 



The summary will not work because you have used parseInt() which returns an integer not a floating value.

You should switch to parseFloat() if you need to include decimal values.

Example below:

var a = '1.5';
var b = '2.3';
var sumInt = parseInt(a) + parseInt(b);
var sumFloat = parseFloat(a) + parseFloat(b);

gs.info('sumint: ' + sumInt);
gs.info('sumfloat: ' + sumFloat);

// output: -->>  sumint: 3
// output: -->>  sumfloat: 3.8

OlaN
Tera Sage
Tera Sage

Hi,

The OOB number validation is just that, it validates that you only have entered numerical values. Only [0-9] are allowed.

You can easily create your own custom validation by navigating to:
Service catalog > Catalog variables > Variable Validation regex

 

A regex that allows for both digits and a dot in between can look something like this:

[\d]+([.][\d]+)?