0

I have this snippet of code in my html file:

<input autocomplete="off" autofocus name="month" placeholder="Month" type="number" onkeydown="return event.keyCode !== 69">

It works exactly as I want it to - doesn't let the user put "e" in a form when they type in their birthday. But how exactly does it work? What does "!== 69" return, and how does it prevent the letter "e" from appearing on the screen?

Mxrvt
  • 1
  • `return false` cancels the keydown event and thus prevents the user from entering the character. – MaartenDev Nov 03 '21 at 20:55
  • This is the kind of JS basics that the web can already be searched for. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) for an explanation on how comparisons work. – Mike 'Pomax' Kamermans Nov 03 '21 at 20:55
  • `69` is an [ASCII code](https://www.asciitable.com/) for "E". – PM 77-1 Nov 03 '21 at 20:57
  • Do you not understand what `!==` is, how keycodes work, or where to find what the keycode `69` means? – Bergi Nov 03 '21 at 21:06
  • 1
    Btw, you can still paste an `e` into that input – Bergi Nov 03 '21 at 21:06
  • @Bergi I did not understand !== and what it returns, but now I do, thanks to other answers. In C we used == and != in conditions so it threw me off at the beginning. But the main question was what happens when the key is pressed and why the letter doesn't appear in the form. MaartenDev's explanation was what I wanted to know. Thanks everyone, I don't know how to upvote and close question – Mxrvt Nov 03 '21 at 23:01
  • @Mxrvt No need to do anything, it's already closed – Bergi Nov 03 '21 at 23:29
  • A more explicit answer: 1) `event.KeyCode` is the [ASCII value](https://www.w3schools.com/charsets/ref_html_ascii.asp) of the key pressed. ASCII value 69 is the character `E` (or `e` as seen by the [keydown](https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event) event 2) `===` means [strictly equal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) and `!==` means not strictly equal 3) returning anything but `true` from the keydown event cancels it. In other words `if key = e then cancel keydown else do nothing` – PoorlyWrittenCode Nov 06 '21 at 19:57

0 Answers0