Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  197] [ 7]  / answers: 1 / hits: 40784  / 11 Years ago, tue, june 11, 2013, 12:00:00

i read many tutorials but i dont know how to do this, this is the input



input(type=text,name=price,id=pricedata-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??


More From » knockout.js

 Answers
58

Is better to create custom binding http://knockoutjs.com/documentation/custom-bindings.html which accept only allowed characters [0-9,.] as numeric representation.



put this line into your view



<input id=text type=text data-bind=numeric, value: number>


put this line into your model (remember to bind number as observable property)



ko.bindingHandlers.numeric = {
init: function (element, valueAccessor) {
$(element).on(keydown, function (event) {
// Allow: backspace, delete, tab, escape, and enter
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: . ,
(event.keyCode == 188 || event.keyCode == 190 || event.keyCode == 110) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
}
};

[#77683] Monday, June 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nestorjarettg

Total Points: 451
Total Questions: 108
Total Answers: 108

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;