Validate decimal field value should be in format of .10 for example

bdoug
Kilo Expert

I have a decimal field and I want to validate the field to ensure the data is in the format of .99.   In other words a decimal point and at most 2 digits behind it, no digits should be in front of the decimal point either.   Any help that you can offer would be greatly appreciated.

1 ACCEPTED SOLUTION

rob_pastore
ServiceNow Employee
ServiceNow Employee

you can use toFixed(2) to round to two decimal points.



JavaScript toFixed() Method



if you want to validate there is nothing before the decimal point i would just check that x < 1   (and > 0 if negatives are not allowed)



If you don't want to round the values, you can validate using .length


View solution in original post

4 REPLIES 4

Abhinay Erra
Giga Sage

here you go



here num= your decimal value;



var number=parseFloat(num.toString().substring(num.toString().indexOf('.'))).toFixed(2);


rob_pastore
ServiceNow Employee
ServiceNow Employee

you can use toFixed(2) to round to two decimal points.



JavaScript toFixed() Method



if you want to validate there is nothing before the decimal point i would just check that x < 1   (and > 0 if negatives are not allowed)



If you don't want to round the values, you can validate using .length


You all pointed me in the right direction.   Your suggestion to ensure the value is between 0 and 1 is exactly what I need.   Thanks again.


LOL. I missed it. That was a low hanging fruit. I have misread your requirements. My code was to set the value to only 2 decimals after decimal point and nothing in front of it.