0

I have a input of number type of my HTML page. This is very close to this one example. Unfortunately this input accepts values like '---', '+++', 'eee' and other non-numeric input.

My task is disable the ability of the control to enter non-numerics like '----'.

Also possible to show a error message when user haves the form. Is any way to do that?

sluge
  • 569
  • 1
  • 5
  • 13

1 Answers1

4

If you specify the input type as number it won't accept non-numeric characters

KEYCODE 8 and 46 are to allow backspace and delete.

Try inputting non-numeric characters in the text box below

<input type="number" onkeydown="javascript: return event.keyCode === 8 || event.keyCode === 46 ? true : !isNaN(Number(event.key))" />
kooskoos
  • 4,622
  • 1
  • 12
  • 29