Haven
Haven

Reputation: 45

Knockout Validation: HTML5 min/max parsed as string

It appears that KO validation plugin parses HTML5 number input attributes (e.g., min, max) as string, as opposed to as number. This results in that number inputs are incorrectly determined to be invalid when they have min/max attributes. See: http://jsfiddle.net/hTvsj/.

HTML:

<label>Some number, valid between 0 and 10</label>
<input type="number" required min="0" max="10" data-bind="value: someNum"/>

JS:

function vm(){
    self=this;
    self.someNum=ko.observable("5");
}
ko.validation.configure({
    parseInputAttributes: true,
});
ko.applyBindings(ko.validatedObservable(new vm()));

Upvotes: 2

Views: 2113

Answers (1)

CodeThug
CodeThug

Reputation: 3202

This was a bug in Knockout Validation. It was fixed with pull request 355 and released with version 2.0.0.

Please note that for this to work, the value in the observable will need to be stored as a number, not as a string. You can use the numeric extender (found at http://knockoutjs.com/documentation/extenders.html) to force values that are typed in to be stored as numbers rather than as strings.

Upvotes: 2

Related Questions