Reputation: 32066
I'm using a custom observable, numericObservable
, based on this, which forces knockout to serialize numeric fields as numbers in json, not strings; I've got that part working.
I've added validation to enforce number
as the field type for port:
self.port = ko.numericObservable(22).extend({ number: true });
..but validation is not working 100%. It seems to pick up non-numerics sometimes, other times it just fails to recognize. If I clear the value for port, switch focus to another field, then come back and enter a number, it works.
http://jsfiddle.net/SAFX/q4QCY/13/
If I use validation against a plain observable, like below, it works, so I suspect the issue may be with extending numericObservable
. Also, the example from which I got numericObservable
uses ko 2.2.2
, my fiddle uses ko 3.0.0
.
//works
self.port = ko.observable(22).extend({ number: true });
Upvotes: 2
Views: 2259
Reputation: 971
Unfortunately parseFloat
function just ignores non numeric symbols. Your code line must use "+" to avoid this:
var parsedValue = parseFloat(+newValue);
Upvotes: 2