I have a JavaScript function that clears a text input if an alphabet character is typed into it.
input = document.getElementById('inf');
input.onkeyup = function() {
  value = input.value;
  truth = isNaN(value);
  if (truth) {
    input.value = '';
  }
};
input {
  border: 2px solid black;
  margin-right: 10px;
}
<input type='text' id='inf' />Numbers Only
<br />Type a number in, and it stays. Put a letter in, and the input clears itself
The problem is, this does not work when the input type is set to number, as evidenced by the below sample.
input=document.getElementById('inf');
input.onkeyup=function(){
  value=input.value;
  truth=isNaN(value);
  if(truth){
    input.value='';
  }
  
};
input{
  border:2px solid black;
  }
<input type='number' id='inf'/>
<br />
As you can see, it doesnt work anymore.
My question is twofold:
1) Why does it work with a text input but not a number input?
2)Is there an easy fix? I need it as a number input, so that must stay the same.
Please Javascript answers only. No jQuery.