Search code examples
htmlangularnumberslimitdigits

Issue with <input type="number"> In Angular


not taking long digits, after 17 or 18 th digit,the entered digits changes to zero so My requirement is to limit the digits to 15, in a reusable way.

<input type="number" class="form-control"(keypress)="omit_special_char($event)"
omit_special_char(event,c?:any) {
           const charCode = (event.which) ? event.which : event.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45) {
        return false;
      }
      return true;
    }`


Solution

  • You should not use the number input type if you are not actually dealing with numbers:

    The type=number state is not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number.

    The HTML Standard on number

    The value of <input type="number"> is a Number.

    When expressed in decimal, the Number.MAX_VALUE runs up to 16 digits, that’s why your code fails.

    See also this question: Why max digits with decimal in JavaScript are only 16

    To solve this, use a more correct input type along with some indicators to provoke a touch keyboard optimised for numerics:

    <label>Something like a number, but not quite
    <input type="text" inputmode="numeric">
    </label>