Honestly, I think the best way is to just user an html5 number
input field:
<input type="number" id="cost" data-bind="value: selectedChoiceValue" />
If you want a knockout solution, You could use an observable/computed combo like Roy J's answer, or you could use a single observable and subscribe and trottle:
var vm = function(){
this.selectedChoiceValue = ko.observable('');
// Just to make sure things do go crazy
this.selectedChoiceValue.extend({ rateLimit: 10 }); // in ms
this.selectedChoiceValue.subscribe(function(newValue){
// Assuming you'll want to also parse out other invalid characters
newValue = yourFunctionToParseTheInput(newValue);
// Update this value to be the parsed valued
this.selectedChoiceValue(newValue);
}, this);
}
function yourFunctionToParseTheInput(value) {
return value.replace('$', '');
}
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<span class="currency">$<input type="text" id="cost" data-bind="value: selectedChoiceValue, valueUpdate:['afterkeydown']" /></span>
Restricting input to textbox: allowing only numbers and decimal point has some good ways to parse the input value as well.