i read many tutorials but i dont know how to do this, this is the input
input(type="text",name="price",id="price"data-bind="text: price,valueUpdate:['afterkeydown','propertychange','input']")
and this is my viewModel
price: ko.computed(function()
{
return parseFloat(this.replace(' ','').replace(/[^0-9\.]+/g,"")) || '';
},this)
but this cause error: this has no method replace??? how can i pass the price value to the computed function??
I had a similar problem.
I also needed to ensure inter values only, and for IE9 and up (so type=numberical was not enough), and since we have a lot of international customers, i could not rely on keycodes either, so the following is what i ended up with:
I know this question is a year old but let me post this for the sake of feature visitor of the page.
Check this out:
Best for today's scenerio https://github.com/Knockout-Contrib/Knockout-Validation
run the snippet below. entering a non digit or something more than 255 will cause the message to display.
courtesy: Bryan Dellinger: https://stackoverflow.com/a/42940109/3868653
An alternative approach: I have found that Knockout works well in combination with jQuery-validate. You just need to make sure that you validate the form before you attempt to use the numeric value.
Say you have a form DOM element, you can set up validation rules via
In your viewmodel you set the two-way binding up as usual, e.g. via
self.year = ko.observable("")
. Now make sure that you call$(".yourform").valid()
before you are further processingself.year()
. In my case, I am doingvar year = parseInt(self.year(), 10)
. Right after form validation this is expected to always produce a meaningful result.With the help of "keydown" event we can restrict other key's in text box which should hold numeric values.
create AlphaCheck Function and add that.
That will works!