0

In Firefox you can enter letters, they show in the screen but not in the js code, about the value. I would like just restrict that the user can put in letters in the input field. Just restrict it to numbers.

https://jsfiddle.net/j5fv4htp/1/

  <input step="0.1" class="number" name="SupplyAirFlow" value="" pattern=".{1,}" type="number">


document.querySelector("input.number").onkeydown = function() {
        console.log("value: ", this.value); 
}; 
marko
  • 10,684
  • 17
  • 71
  • 92
  • Possible duplicate of : https://stackoverflow.com/questions/18156824/restricting-an-input-box-to-only-numbers-0-9 – Victor Mar 08 '18 at 16:48

1 Answers1

0

If you want only numbers in the input use:

document.getElementsByClassName('number')[0].addEventListener('keydown', function(e) {
    var key   = e.keyCode ? e.keyCode : e.which;

    if (!( [8, 9, 13, 27, 46, 110, 190].indexOf(key) !== -1 ||
         (key == 65 && ( e.ctrlKey || e.metaKey  ) ) || 
         (key >= 35 && key <= 40) ||
         (key >= 48 && key <= 57 && !(e.shiftKey || e.altKey)) ||
         (key >= 96 && key <= 105)
       )) e.preventDefault();
});
Victor
  • 468
  • 1
  • 5
  • 17