# How to allow numbers, backspace, delete, left and right arrow keys in the html text?

**URL:** <https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [August 7, 2019, 11:23am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339 "2019-08-07T11:23:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChinosoCodeTrybes](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chinosocodetrybes/32/113410_2.png) [@ChinosoCodeTrybes](https://forum.freecodecamp.org/u/ChinosoCodeTrybes)\
**Post date:** [August 7, 2019, 11:23am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/1 "2019-08-07T11:23:35Z")

</div>

please i don’t really know what’s going on here, i know that if `window.event(true)` assign `event.keyCode` to key..what does the the next if block do? whats the meaning of `event.keyCode == 8` and the others

```auto
function validateQty(event) {
    var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
 || event.keyCode == 37 || event.keyCode == 39) {
    return true;
}
else if ( key < 48 || key > 57 ) {
    return false;
}
else return true;
};

```

---

<div class="post-metadata">

**Author:** ![ChinosoCodeTrybes](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chinosocodetrybes/32/113410_2.png) [@ChinosoCodeTrybes](https://forum.freecodecamp.org/u/ChinosoCodeTrybes)\
**Post date:** [August 7, 2019, 11:24am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/2 "2019-08-07T11:24:49Z")

</div>

I’ll be applying it to a keydown event

---

<div class="post-metadata">

**Author:** ![JesseHope](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jessehope/32/121926_2.png) [@JesseHope](https://forum.freecodecamp.org/u/JesseHope)\
**Post date:** [August 7, 2019, 11:33am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/3 "2019-08-07T11:33:32Z")

</div>

`if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39)` //If the key that is pressed has keycode 8 or 46 or 37 or39…

`{ return true;}` //… then return true.

`else if ( key < 48 || key > 57 )` //otherwise if the key pressed has keycode less than 48 or greater than 57…  
`{ return false;}`//then return false

`}else return true;};`//otherwise return true

Another way to say this is:  
_return true if the key pressed is: 8, 37, 39, 46, 48-57._  
_return false if any other key._

I think there are better ways to write this and I don’t have the keycodes memorized so I don’t know which keys they’re referring to but that’s whats happening.  
-J

---

<div class="post-metadata">

**Author:** ![ChinosoCodeTrybes](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chinosocodetrybes/32/113410_2.png) [@ChinosoCodeTrybes](https://forum.freecodecamp.org/u/ChinosoCodeTrybes)\
**Post date:** [August 7, 2019, 11:37am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/4 "2019-08-07T11:37:26Z")

</div>

so the `keyCode` is like a character that represents a keyboard key,also what does the `return true` represent

---

<div class="post-metadata">

**Author:** ![JesseHope](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jessehope/32/121926_2.png) [@JesseHope](https://forum.freecodecamp.org/u/JesseHope)\
**Post date:** [August 7, 2019, 11:42am UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/5 "2019-08-07T11:42:26Z")

</div>

Right, each key on the keyboard has a `keyCode`. Capital letters are different from lowercase letters. For example, `keyCode: 8` is “backspace”. You can google them to look them up.

`return true` returns a `boolean` value of `true`. It’s a simple true/false value. Usually you take that and do something else with it.  
-J

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:23pm UTC](https://forum.freecodecamp.org/t/how-to-allow-numbers-backspace-delete-left-and-right-arrow-keys-in-the-html-text/303339/6 "2021-01-25T12:23:35Z")

</div>

Closed on January 25, 2021, 12:23pm UTC


