문제

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.

Fiddle

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 });
도움이 되었습니까?

해결책

Unfortunately parseFloat function just ignores non numeric symbols. Your code line must use "+" to avoid this:

var parsedValue = parseFloat(+newValue);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top