Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. <input type="text" class="input">
  2.  
  3. <script>
  4. let input = document.querySelector('input');
  5.  
  6. input.addEventListener('keydown', e => {
  7. let isDigit = false;
  8. let backSpace = false;
  9. let isDash = false;
  10.  
  11. if (e.key >= 0 || e.key <= 9) {
  12. isDigit = true;
  13. }
  14.  
  15. if (e.key === 'Backspace' || e.key === '-') {
  16. backSpace = true;
  17. isDash = true;
  18. }
  19.  
  20. if (!isDigit && !backSpace && !isDash) {
  21. e.preventDefault();
  22. }
  23. });
  24. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement