JSFiddle - React, Tailwind, and code Playground

by Andrew Gerst

HTML

<input id="textfield" type="text"/>

JavaScript

$("#textfield").on("keypress blur", function(e){
    if (e.type === "keypress")
        return !!String.fromCharCode(e.which).match(/^\d$/);
    this.value = this.value.replace(/[^\d].+/, "");
});