2

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 });
Community
  • 1
  • 1
raffian
  • 31,267
  • 26
  • 103
  • 174

1 Answers1

2

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

var parsedValue = parseFloat(+newValue);
Pavel Kutakov
  • 971
  • 1
  • 7
  • 11
  • 1
    upon further anaylsis, this is working. I switched to `parseInt(+newValue)`, don't need floats. Im doing some additional testing to make sure the validation is working, ty – raffian Dec 18 '13 at 18:36