How to allow decimal values in integer field

Siri8
Kilo Contributor

In a Integer type field it should allow decimal values as well.I have followed below script but it is not accepting decimal values

Client Script : onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate)

{

if (isLoading || newValue === '')

{
return;
}
var reg = /^[0-9]*\.[0-9]{2}$/;
var ans = g_form.getValue('field_name');
if(!reg.test(ans))
{
g_form.clearValue('field_name');
}
}

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Hi Siri,

 

Since its integer field it will not accept decimal values. Even if you manage it will round-off decimal.

You can use string field if required & then use the .toFixed(2) to get two decimal values.

Pratiksha Kalam
Kilo Sage

Hello,

You can do an onBefore business rule (insert/update) which gets the value of your decimal field and fixes it to two decimal places:

function onBefore(current, previous) {

var getDec = current.YOUR_DECIMAL_FIELD.toFixed(2);

  current.YOUR_DECIMAL_FIELD = getDec;

}

If answer is helpful mark correct!

Thanks,

Pratiksha

 

Its not working

Tejas Tamboli
Giga Guru

Hello Siri,

Please refer below script and do changes accordingly in your onChange client script.

 

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

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

 

   if(!regexp.test(newValue))  {

        alert('Only numbers and dot value allowed');

        g_form.clearValue('u_field_name');

        g_form.setValue('u_field_name','');

  }

 

 

I hope this will help you.

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.

Thanks,
Tejas