Allow only numbers in input field with JavaScript

preview_player
Показать описание
Here is a really quick and simple way to to make sure only numbers can be typed into an input field using JavaScript. #javascript #webdeveloper #webdevelopment #webdev #programming #html
Рекомендации по теме
Комментарии
Автор

On the paper its a great idea but in practice it is extremely bad because it disable all default keys and you have to manually made exceptions for all of them.

if the user is blind he wont be able to navigate with tab
if you made a mistake you can't use the arrow keys
if you want to confirm the enter shortcut is disable
...

So yes if you want to allow "only numbers" this is the way to go, but if you want to allow "numbers AND utils" use a regex + the replace function :
val.replace(/[^0-9]/g, '');

QuentinPAULEAU
join shbcf.ru