Add character in an input

2

The person types their height (which always has three digits). I want autocomplete, example: -

The person types- 200

And the software will convert to 2.00

    
asked by anonymous 28.10.2017 / 07:54

1 answer

2

If you always have 3 digits, then limit the input with the maxlength attribute that specifies the maximum number of characters that the user can enter.

<input class="number" maxlength="3">

Javascript

It does not allow typing different values of numbers and after entering the third number, inserts the comma automatically after the first number entered.

var el = document.querySelector('input.number');
el.addEventListener('keyup', function (event) {
  if (event.which >= 37 && event.which <= 40) return;

  this.value = this.value.replace(/\D/g, '').replace(/\B(?=(\d{2})+(?!\d))/g, ',');
});
<input class="number" maxlength=3>
    
28.10.2017 / 19:41