Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

How can I allow users to overwrite the numbers in the input control (when there are already two numbers)?

$
0
0

Please see the JavaScript below:

maxLengthDay(event) {
    const maxLength = 2;
    if (event.target.value.length + 1 > maxLength)
      return false;
    else
      return true;
  }

and the HTML below:

<input (keypress)="maxLengthDay($event)" type="number" />

It works as expected i.e. it ensures the input box only contains two characters at most. The problem occurs if the user highlights the number (specifically when there are two numbers) and try to overwrite one or two numbers. Nothing happens because the input control contains two numbers at that point.

How can I allow users to overwrite the numbers in the input control (when there are already two numbers)?


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>