- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 07:15 AM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 07:32 AM
you can use toFixed(2) to round to two decimal points.
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 07:29 AM
here you go
here num= your decimal value;
var number=parseFloat(num.toString().substring(num.toString().indexOf('.'))).toFixed(2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 07:32 AM
you can use toFixed(2) to round to two decimal points.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 07:55 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2016 08:01 AM
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.