JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" id="myInput" />
JavaScript
var $input = $("#myInput");
$input.keyup(function (e) {
var currentLength = $input.val().length;
var c = e.which;
console.log("key", c);
//0=48, 9=57, 37-40 = arrow keys 8= backspace, 46=delete
if (c === 8 || c === 46 || (c >= 37 && c <= 40)) {
//delete, backspace, and arrow keys are allowed
} else if (c < 48 || c > 57 || currentLength >= 3) {
e.preventDefault();
}
}).on("paste", function (e) {
e.preventDefault();
});