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
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.
In the end I made a jQuery plugin that will format the
<input type="number" />
appropriately for me. I also noticed on some mobile devices themin
andmax
attributes don't actually prevent you from entering lower or higher numbers than specified, so the plugin will account for that too. Below is the code and an example:You guys are completely right numbers can only go in the numeric field. I use the exact same thing as already listed with a bit of css styling on a span tag:
Then add a bit of styling magic:
The browser only allows numerical inputs when the type is set to "number". Details here.
You can use the type="text" and filter out any other than numerical input using JavaScript like descripted here
It seems that you'll need two fields, a choice list for the currency and a number field for the value.
A common technique in such case is to use a div or span for the display (form fields offscreen), and on click switch to the form elements for editing.
Add
step="0.01"
to the<input type="number" />
parameters: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.