Tom Studee
Tom Studee

Reputation: 10452

Why does my string value pass knockout validation, but integer value does not?

Using knockout 2.2.1 with latest knockout-validation.js.

See the following jsfiddle for a working example of my issue:

http://jsfiddle.net/tbstudee/keRPY/4/

As you can see when being loaded with existing data the integer value is not passing validation, but the string value is. This is only happening when the viewModel is initialized with existing data.

I know the problem is in my isValid flag since commenting it out removes the validation error on load.

self.isValid = ko.computed(function () {
    return ko.validation.group(
        self,
        {
            observable: true,
            deep: true
        }).showAllMessages(true);
}, self);

What can I do to work around this, other than seed my viewModel with strings instead of ints?

Upvotes: 1

Views: 1008

Answers (1)

Kyeotic
Kyeotic

Reputation: 19847

maxLength and minLength are for strings, for numbers you need to use min and max, otherwise the validation ends up casting the value as a string. Take a look at this fiddle

self.myInt = ko.observable(data.myInt).extend({
        required: true,
        number: true,
        min: 100000,
        max: 999999
    });

Upvotes: 1

Related Questions