JSFiddle - React, Tailwind, and code Playground

by GuillaumeCisco

HTML

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

JavaScript

(function($) {
    $.fn.numeric = function(options) {
        return this.each(function() {
            var $this = $(this);

            $this.keypress(options, function(e) {
                // allow backspace and delete
                if (e.which == 8 || e.which == 0) {
                    return true;
                }

                // allow float
                if (e.which == 46 && this.value.length >=1 && this.value.indexOf('.') == -1) {
                    return true;
                }

                //if the letter is not digit
                if (e.which < 48 || e.which > 57) return false;

                // check max range
                var dest = e.which - 48;
                var result = this.value + dest.toString();
                if (result > e.data.max) {
                    return false;
                }
            });
        });
    };
})(jQuery);

$('#input').numeric({
    max: 999
});