JSFiddle - React, Tailwind, and code Playground

HTML

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

JavaScript

$('#test').on('input', function() {

    var oldVal = $(this).val();

    // remove everything but digits
    var newVal = oldVal.replace(/[^\d]/g, '');
    
    // put leading minus back in place (if there was one)
    if(oldVal.trim().length > 0 && oldVal.trim()[0] == '-') {
        newVal = '-' + newVal;
    }
    
    $(this).val(newVal);

});