Validation on a Decimal field

srivatsa
Giga Contributor

Hi,

I have a decimal field where i want to give out an alert message when the number entered has more than 3 decimal places i.e. if the input for a decimal field is 13.4567, i want an alert message to pop up 

Can regex validation be done on a decimal field? 

PFB the onchange script (for the decimal field)  i wrote which isn't working, any modifications to this? 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var countDecimals = function(value) {
if (Math.floor(value) !== value)
return value.toString().split(".")[1].length || 0;
return 0;
}
alert(newValue);
if (countDecimals(newValue) != '2') {
alert('should have 2 decimal places');
}
}

1 ACCEPTED SOLUTION

Dhananjay Pawar
Kilo Sage

Hi,

Here is sample script.

 

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

var sdVal=g_form.getValue('short_description');
var splitVal=sdVal.split('.');
alert("First = "+splitVal[0]+"second = "+splitVal[1]);
var len=splitVal[1].length;
alert(len);
if(len>3){
alert('wrong decimal value');
}
else{
alert('correct decimal value');
}

}

 

Note - Make changes accordingly as per need.

Mark correct/helpful based on impact.

Thanks,

Dhananjay.

View solution in original post

3 REPLIES 3

Dhruv Gupta1
Kilo Sage
Kilo Sage
Instead of replying fix the decimal to 2 places using fixed()

Dhananjay Pawar
Kilo Sage

Hi,

Here is sample script.

 

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

var sdVal=g_form.getValue('short_description');
var splitVal=sdVal.split('.');
alert("First = "+splitVal[0]+"second = "+splitVal[1]);
var len=splitVal[1].length;
alert(len);
if(len>3){
alert('wrong decimal value');
}
else{
alert('correct decimal value');
}

}

 

Note - Make changes accordingly as per need.

Mark correct/helpful based on impact.

Thanks,

Dhananjay.

Thanks!