Knockout-Validation
Knockout-Validation copied to clipboard
Update to handle small steps - ie: 0.0000001
I was having issues with very, very small step increments. Updated so that the code now will handle small steps by multiplying by that step amount to convert to a handleable number.
Example: var testObj = ko.observable('') .extend({ step: 0.00000003 });
testObj(0.00001115);
testObj.isValid() != true;
Hi, I solved it this way
validation.rules['step'] = {
validator: function (value, step) {
var values = (value).toString().split(".");
var steps = (step).toString().split(".");
if (kv.utils.isEmptyVal(val) || step === 'any') { return true; }
if (values.length <= 1 && steps.length <= 1) {
return (value % step) == 0;
}
var decimals = (values.length > 1) ? values[1].split("").length : steps[1].split("").length;
var multiplier = 10 ** decimals;
return Math.round(value * multiplier) % (step * multiplier) < 1
},
message: 'The value must increment by {0}.'
};
https://jsfiddle.net/v5zm18s7/1/