-1

I have an HTML input field but if the user keeps on pressing a key, it will add the same letter non-stop. I want to suppress this behavior.

How can I override the input component's input event listener to not allow to not add successive characters when the key is being kept pressed down?

Axel Carré
  • 145
  • 1
  • 8
  • what u tried so far? share some code – ericmp Dec 19 '22 at 12:50
  • 2
    Does this answer your question? [How to disable repetitive keydown in JavaScript](https://stackoverflow.com/questions/17514798/how-to-disable-repetitive-keydown-in-javascript) – Lain Dec 19 '22 at 12:57

2 Answers2

-2
<input type="text" oninput="this.value = this.value.replace(/(.)\1+/g, '$1')" />

https://jsfiddle.net/sxwr2u3z/

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
-2

Another answer. This one uses KeyboardEvent.repeat to detect continuous pressing.

const input = document.querySelector('input');

input.addEventListener('input', function (event) {
    this.value = this.value.replace(/.$/,'');
});
input.addEventListener('keyup', function (event) {
    if (event.repeat) return;
  this.value += event.key;
});
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40