JSFiddle - React, Tailwind, and code Playground

by dceast

HTML

<input type="text" data-bind="value: myNum, valueUpdate: 'afterkeyup'" />

JavaScript

(function(ko) {
    
    ko.observable.fn.numeric = function () {
        var target = this;

        this.subscribe(function() {
           var value = target();
           target(value.replace(/[^0-9]+/g,'')); //[^0-9\.]/g - allows decimals
        }, this);
        
        return target;
    };
    
    function ViewModel() {
        this.myNum = ko.observable().numeric();
    };
    
    ko.applyBindings(new ViewModel());
    
})(ko);