Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format number in an HTML5 input tag

Tags:

html

This is my input

<input type="number" id="inputNumbers" step="0.01" ></input>

Is it possible to only allow users to enter numbers that match the pattern '0.00', like the way I declare it in the step attribute, without using JavaScript?

Example: If the user types '1.23' and then tries to enter a new number, decimal or comma it can't and the number stays at '1.23'.

If there's not a way to match the step attribute, Is it possible to only allow one decimal in the number?

like image 373
Marcus Höglund Avatar asked Jun 28 '16 13:06

Marcus Höglund


People also ask

How do you add a number to an HTML input?

The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed. min - specifies the minimum value allowed.

Is input an format tag in HTML?

The input tag is used within < form> element to declare input controls that allow users to input data. An input field can be of various types depending upon the attribute type. The Input tag is an empty element which only contains attributes.

Is number a valid input type in html5?

With the number input type, you can constrain the minimum and maximum values allowed by setting the min and max attributes. You can also use the step attribute to set the increment increase and decrease caused by pressing the spinner buttons. By default, the number input type only validates if the number is an integer.

How do I put 2 decimal places in HTML?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result. Copied!


1 Answers

I think you can try something like this:

input type="number" name="number" pattern="([0-9]{1,3}).([0-9]{1,3})" title="Must contain a decimal number">

see here

Added Plunkr: Plunker

like image 159
nouseforname Avatar answered Sep 21 '22 10:09

nouseforname