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?

2
  • add javascript onChange event and do validation over there. Commented Mar 16, 2020 at 9:54
  • Use regular expression and validate the input on the input event @sluge Commented Mar 16, 2020 at 9:55

1 Answer 1

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))" />

Sign up to request clarification or add additional context in comments.

6 Comments

How else would you allow negative nos? Also e stands for exponent that's why it is allowed.
If you stiil don't want them try the updated answer
Glad, I wad able to help:)
Not my question though :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.