Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input type="text" class="input">
- <script>
- let input = document.querySelector('input');
- input.addEventListener('keydown', e => {
- let isDigit = false;
- let backSpace = false;
- let isDash = false;
- if (e.key >= 0 || e.key <= 9) {
- isDigit = true;
- }
- if (e.key === 'Backspace' || e.key === '-') {
- backSpace = true;
- isDash = true;
- }
- if (!isDigit && !backSpace && !isDash) {
- e.preventDefault();
- }
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement