How to find when write not a number? keypress event

Like on an input field

https://jsfiddle.net/d0tx9a1y/

 if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {

What I got if not 8 not 8 less than 48 and more than 57 as I understand bit it works.

So this make fires error when not enter numbers but I not understand what that 8 etc means. Any other clear way?

Or explain?

Thanks!

This page has all of the answers Char codes

In short:
If you attach an observer method like keypress, what is returned to the function is a character code. This code can be many things, but in your case, it is a number. Even when an actual number key is pressed on the keyboard, a different number is returned. So, your function has the if statement to condition the error/warning message when any character code not corresponding to a number is returned.

Hope that helps.

1 Like

Very useful page! Thank you so much!