I know variations of this have been asked, but my requirement is that instead of formatting/rounding the value that\'s entered, I need to not even allow the user to enter mo
Well i used AUTONUMERIC
plugin for number formatting which was reliable and awesome .
I just made a sample fiddle to your requirement check it here
View Model:
var vm = function(){
this.Amount=ko.observable("");
this.OnloadAmount=ko.observable(143); //onLoad Test
ko.bindingHandlers.autoNumeric = {
init: function (el, valueAccessor, bindingsAccessor, viewModel) {
var $el = $(el),
bindings = bindingsAccessor(),
settings = bindings.settings,
value = valueAccessor();
$el.autoNumeric(settings);
$el.autoNumeric('set', parseFloat(ko.utils.unwrapObservable(value()), 10));
$el.change(function () {
value(parseFloat($el.autoNumeric('get'), 10));
});
},
update: function (el, valueAccessor, bindingsAccessor, viewModel) {
var $el = $(el),
newValue = ko.utils.unwrapObservable(valueAccessor()),
elementValue = $el.autoNumeric('get'),
valueHasChanged = (newValue != elementValue);
if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) {
valueHasChanged = true;
}
if (valueHasChanged) {
$el.autoNumeric('set', newValue);
}
}
};
}
ko.applyBindings(new vm());
View :
<input type="text" data-bind="autoNumeric:$data.Amount, settings:{mDec:1,aSep: ''} " />
Here mDec:1
restricts number to decimal palce of one and asep:''
means it will not sperate number with commas etc
PS: we can restrict entering invalid characters and do lots of other stuff with ease .
For complete Reference check here