Skip to content

Conversation

@ebouwsema
Copy link

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;

@gtripoli
Copy link

gtripoli commented Feb 12, 2020

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants