Edit in JSFiddle

<input pattern="\d">

              
$(document).on('change keyup', 'input[pattern]', function() {
  let pattern = new RegExp('[^' + $(this).attr('pattern') + ']', 'g');
  $(this).val($(this).val().replace(pattern, ''));
});