Input[number] accept numbers only in Firefox

Asked

Viewed 398 times

3

How do I make the input[number] field accept only numbers in Firefox? In Chrome, Opera and Edge works perfectly, but in Firefox the[number] field accepts letters and spaces.

inserir a descrição da imagem aqui

<input type="number" name="EstoqueProd[]" class="form-control" min="1" value="1"/>

1 answer

3


You can force this with an event headphone from keydown which detects whether the character entered is a digit by comparing the key code.

An example would be:

var input = document.querySelector('input[name="EstoqueProd[]"]');
input.addEventListener('keydown', function(e) {
  var numero = (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105);
  var controlos = [8, 37, 39].includes(e.keyCode);
  if (!numero && !controlos) return e.preventDefault();
});
<input type="number" name="EstoqueProd[]" class="form-control" min="1" value="1" />

jsFiddle: http://jsfiddle.net/Sergio_fiddle/9unxyknz/1/

  • 1

    Perfect Sergio. It worked. Thank you again!

  • Numeric keypad failed :O

  • @Well viewed DVD, fixed.

  • 1

    Sérgio, Backspace does not work in this solution

  • @Brenocosta actually not even the arrows... here comes another Dit :)

  • 1

    @Brenocosta corrected. Thank you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.