Danny Danny - 3 months ago 38
jQuery Question

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


Is there anyway for me to set the value to something like

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 the min and max 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:

(function($) {
  $.fn.currencyInput = function() {
    this.each(function() {
      var wrapper = $("<div class='currency-input' />");
      $(this).before("<span class='currency-symbol'>$</span>");
      $(this).change(function() {
        var min = parseFloat($(this).attr("min"));
        var max = parseFloat($(this).attr("max"));
        var value = this.valueAsNumber;
        if(value < min)
          value = min;
        else if(value > max)
          value = max;

$(document).ready(function() {
.currency {

.currency-symbol {
  padding: 3px 5px;
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<input type="number" class="currency" min="0.01" max="2500.00" value="25.00" />