0

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;
    }`

1
  • You might be misusing the number input type, the HTML spec explains that it is “not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number.” html.spec.whatwg.org/multipage/…. There are other attributes to control touch screen keyboards like inputmode Commented Nov 29, 2022 at 19:33

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.