Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value to currency in <input type="number" />

I am trying to format a number input by the user into currency using javascript. This works fine on <input type="text" />. However, on <input type="number" /> I cannot seem to be able to set the value to anything that contains non-numeric values. The following fiddle shows my problem

http://jsfiddle.net/2wEe6/72/

Is there anyway for me to set the value to something like $125.00?

I want to use <input type="number" /> so mobile devices know to bring up a keyboard for number input.

like image 230
Danny Avatar asked Feb 01 '13 16:02

Danny


People also ask

How do you input currency in HTML?

One could use both number or text input in order to accept money/decimals. For an input field for currency/money, it is recommended to use input type of number and specify appropriate attributes as outlined above. As of 2020, there is not a W3C spec for an actual input type of currency or money.

How do you change the currency format in HTML?

NumberFormat() Method. One of the best ways in JavaScript to format the number as a currency string is to use Intl. NumberFormat() method. You can pass the locale to the method as a parameter and set the dollar sign before the number and format number.

How do you insert a currency symbol in a text box?

In the Data Type Format dialog box, do one of the following: To add a currency symbol, click Currency, and then in the Currency list, select the type of currency that you want to display. To remove a currency symbol, click Number.


1 Answers

Add step="0.01" to the <input type="number" /> parameters:

<input type="number" min="0.01" step="0.01" max="2500" value="25.67" /> 

Demo: http://jsfiddle.net/uzbjve2u/

But the Dollar sign must stay outside the textbox... every non-numeric or separator charachter will be cropped automatically.

Otherwise you could use a classic textbox, like described here.

like image 67
Andrea Ligios Avatar answered Oct 07 '22 11:10

Andrea Ligios