JavaScript limit input to 2 decimal places | Restrict input example

Use JavaScript for validation input or use step=".01", which allows up to two decimal places on keypress input.

Here are the test cases:

1.25 – Allowed

12.5 – Allowed

125.5 – Allowed

125.55 – Allowed

123.555 – Not Allowed

Examples of JavaScript limit input to 2 decimal places

Example 1: Using JavaScript validation

The input box will not allow a 3rd decimal place. You have also use indexof() and substr() method of JS.

Output:

JavaScript limit input to 2 decimal places

Example 2: Using input “step” attribute

The step allows for any number of decimal places, use step=".01", which allows up to two decimal places.

Here is Simple HTML code:-

Output:

Restrict to 2 decimal places in keypress of a text box

Do comment if you have doubts and suggestions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.