Skip to content Skip to sidebar Skip to footer

I Would Like To Add Numerical Limits To This Block Of Code

document.querySelectorAll('input').forEach(input => { input.addEventListener('keydown', function(e) { var charValue = String.fromCharCode(e.keyCode); if (((!/^(\d+)?([

Solution 1:

Everytime you type a letter in the keyboard, you got a specific e.keyCode or e.key. Try to debug the value of each variable to understand the code.

To put a limit simply do a check to prevent default behavior (prevent further typing)

var nextValue = this.value + e.key;
if (nextValue < 1 || nextValue > 5) {
    e.preventDefault()
}

Post a Comment for "I Would Like To Add Numerical Limits To This Block Of Code"