The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).

Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:

<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">

(I'd also set min=0 if it can only be positive)

If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)

Here's a playground showing how various steps affect various input types:

<form>
  <input type=number step=1 /> Step 1 (default)<br />
  <input type=number step=0.01 /> Step 0.01<br />
  <input type=number step=any /> Step any<br />
  <input type=range step=20 /> Step 20<br />
  <input type=datetime-local step=60 /> Step 60 (default)<br />
  <input type=datetime-local step=1 /> Step 1<br />
  <input type=datetime-local step=any /> Step any<br />
  <input type=datetime-local step=0.001 /> Step 0.001<br />
  <input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
  <input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
  <input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand


As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!

Answer from Dave on Stack Overflow
Top answer
1 of 13
2477

The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).

Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:

<label for="totalAmt">Total Amount</label>
<input type="number" step="0.01" id="totalAmt">

(I'd also set min=0 if it can only be positive)

If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)

Here's a playground showing how various steps affect various input types:

<form>
  <input type=number step=1 /> Step 1 (default)<br />
  <input type=number step=0.01 /> Step 0.01<br />
  <input type=number step=any /> Step any<br />
  <input type=range step=20 /> Step 20<br />
  <input type=datetime-local step=60 /> Step 60 (default)<br />
  <input type=datetime-local step=1 /> Step 1<br />
  <input type=datetime-local step=any /> Step any<br />
  <input type=datetime-local step=0.001 /> Step 0.001<br />
  <input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
  <input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
  <input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
</form>
Run code snippetEdit code snippet Hide Results Copy to answer Expand


As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!

2 of 13
219

Via: http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/

But what if you want all the numbers to be valid, integers and decimals alike? In this case, set step to “any”

<input type="number" step="any" />

Works for me in Chrome, not tested in other browsers.

🌐
W3Resource
w3resource.com › javascript › form › decimal-numbers.php
JavaScript : Checking for Floating numbers - w3resource
August 19, 2022 - Javascript function to check if a field input contains numbers as well as decimal numbers.
🌐
Quora
quora.com › How-do-I-take-an-input-type-float-number-in-HTML
How to take an input type float number in HTML - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Top answer
1 of 7
41

I use this - works for keyboard input or copy and paste

$('input.float').on('input', function() {
  this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<input type="text" class="float" />

Explanation:

  • First regex replaces anything that's not a number or a decimal.
  • Second regex removes any instance of a second decimal.
2 of 7
8

I filter the first position input with the jQuery Caret plugin. Otherwise, once the dot is typed, it's already late to check where it was placed. I tried checking for the dot, then deleting the dot, but it does not look nice.

jQuery caret plugin: http://examplet.buss.hk/js/jquery.caret.min.js

What I did:

http://jsfiddle.net/FCWrE/422/

Try it :)

$('.filterme').keypress(function(eve) {
  if ((eve.which != 46 || $(this).val().indexOf('.') != -1) && (eve.which < 48 || eve.which > 57) || (eve.which == 46 && $(this).caret().start == 0)) {
    eve.preventDefault();
  }

  // this part is when left part of number is deleted and leaves a . in the leftmost position. For example, 33.25, then 33 is deleted
  $('.filterme').keyup(function(eve) {
    if ($(this).val().indexOf('.') == 0) {
      $(this).val($(this).val().substring(1));
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/caret/1.0.0/jquery.caret.min.js"></script>
<input type="text" class="filterme">

🌐
AlgoTree
algotree.org › algorithms › a pinch of code
JavaScript : Accept only a positive integer / float input :: AlgoTree
To accept only a numeric value ( integer or float ) value in the HTML text field we make use of the following · onkeypress : An event occurs when the user presses a key. event.key : The key property of an event returns the identifier of the key that was pressed. isFinite : This function determines if the entered input is a finite number. <string> . indexOf (".") : Checks if a decimal is present in the text field. Javascript : Program to accept only a decimal / float input.
🌐
Stack Overflow
stackoverflow.com › questions › 27101497 › javascript-how-to-get-a-float-value-from-an-html-input
Javascript - how to get a float value from an HTML input? - Stack Overflow
May 24, 2017 - function which works fine for integer numbers, but seems I cannot process the float values into the Javascript function. I don't know where I am wrong. Thank you in advance for your help. EDIT: I finally solved the problem. I was missing an apostrophe in the PHP file... Bye. javascript · input ·
🌐
Erikmartinjordan
erikmartinjordan.com › input-float-numbers-react
Create an input allowing only float numbers in React — Erik Martín Jordán
import React, { useState } from 'react' const floatInput = () => { const [number, setNumber] = useState('') const handleNumber = (e) => { let input = e.target.value if(input.match(/^([0-9]{1,})?(\.)?([0-9]{1,})?$/)) setNumber(input) } const handleFloat = () => { // The conditional prevents parseFloat(null) = NaN (when the user deletes the input) setNumber(parseFloat(number) || '') } return <input placeholder = 'Type number...' value = {number} onChange = {handleNumber} onBlur = {handleFloat}/> }
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-set-float-input-type-in-html5
How to set float input type in HTML5?
While HTML5 lacks a dedicated float input type, the inputmode="decimal" approach offers excellent mobile usability, while the step attribute with type="number" provides precise decimal control and validation.
🌐
GitHub
github.com › code4fukui › input-float
GitHub - code4fukui/input-float · GitHub
<script type="module" src="https://code4fukui.github.io/input-float/input-float.js"></script> <input-float id=inp maxlength=5></input-float> <script type="module"> onload = () => { inp.onchange = () => { console.log(inp.value); }; }; </script>
Author: code4fukui
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-set-input-box-to-be-a-floating-number-in-reactjs
How to set input box to be a floating number in ReactJS ? | GeeksforGeeks
November 6, 2023 - We are going to learn how to create a translucent text input in React JS. We are going to create a translucent animated text input using framer-motion and styled components.Prerequisites:JavaScript (ES6)React JSReact useStatestyled-componentsframer-motion Approach: We are going to create a transluce
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
The parseFloat() method parses a value as a string and returns the first number.
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
🌐
jQuery Script
jqueryscript.net › jquery plugins › jquery form plugins
Restrict Input Fields to Floatings - jQuery TUS.Float | Free jQuery Plugins
June 23, 2023 - 1. Download and load the TUS.Float plugin in the document. <script src="/path/to/cdn/jquery.slim.min.js"></script> <script src="/path/to/jquery.tusfloat.js"></script> 2. Apply the plugin to an input element and specify the decimal limit.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-into-float-in-javascript
Converting string into float in JavaScript - GeeksforGeeks
February 25, 2026 - In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Element › input › number
<input type="number"> HTML attribute value - HTML | MDN
It is not as semantically useful as other ways to explain your form, and can cause unexpected technical issues with your content. See <input> labels for more information. A Boolean attribute which, if present, means this field cannot be edited by the user. Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement value property.
🌐
Stack Overflow
stackoverflow.com › questions › 58341003 › read-html-input-type-number-as-int-float-via-js
javascript - Read HTML input type="number" as int/float via JS - Stack Overflow
This question is not about submitting the form via http GET/POST. It is about reading the input elements via JS. ... JavaScript does not distinguish between integers and floats.