Home > Enterprise >  I would like to add numerical limits to this block of code
I would like to add numerical limits to this block of code

Time:10-10

document.querySelectorAll('input').forEach(input => {
  input.addEventListener("keydown", function(e) {
    var charValue = String.fromCharCode(e.keyCode);
    if (((!/^(\d )?([.]?\d{0,1})?$/.test(this.value   e.key)) && (e.which != 8))) {
      e.preventDefault()
    }
  })
})

SetPoint :<input id="input" type="text" name="setPoint"   max="5" min="1" />

this block of code is a bit beyond my understanding but I'm guessing you use e.something and <= in the if statement to add a less than or equal to limit the number that the form will accept

CodePudding user response:

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()
}
  • Related