Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Input type number Thousand separator

I want to have a thousand separator (e.g. 1,000,000) in my Input field. However, it has to be of type number because I need to be able to adjust its value using "step". Code:

<input type="number" id='myNumber' value="40,000" step='100'> 

I tried using Javascript to adjust the value but didn't work. Any help would be appreciated. Thanks!

like image 1000
tije Avatar asked Aug 06 '15 23:08

tije


People also ask

How do you add a thousand separator in HTML?

It needs to add x1=x1. replace(\,\g,'') to work correctly when the user changes the number.

How do you put a comma in input type numbers?

You can use type="text" and then add commas to the number in the onchange event.

Can input type be number in HTML?

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.

How do you set a max number in HTML?

The max attribute specifies the maximum value for an <input> element. Tip: Use the max attribute together with the min attribute to create a range of legal values. Note: The max and min attributes works with the following input types: number, range, date, datetime-local, month, time and week.


2 Answers

Using autoNumeric plugin you can made a field as numeric input with different separators.

Include plugin:

<script src="~/Scripts/autoNumeric/autoNumeric.min.js" type="text/javascript"></script> 

Html:

 <input type="text" id="DEMO" data-a-sign="" data-a-dec="," data-a-sep="." class="form-control">  

Script:

<script>  jQuery(function($) { $('#DEMO').autoNumeric('init');     }); </script> 

You can type only number, if you input 100000,99 you will see 100.000,99.

More: https://github.com/autoNumeric/autoNumeric

like image 124
Hawlett Avatar answered Sep 20 '22 12:09

Hawlett


  • Check this webdesign.tutsplus.com tutorial
  • Final result is summarized here (look at direct Codepen playground)

    $("#formInput".on("keyup", function(event ) {                        // When user select text in the document, also abort.     var selection = window.getSelection().toString();      if (selection !== '') {         return;      }            // When the arrow keys are pressed, abort.     if ($.inArray(event.keyCode, [38, 40, 37, 39]) !== -1) {         return;      }            var $this = $(this);                 // Get the value.     var input = $this.val();                 input = input.replace(/[\D\s\._\-]+/g, "");      input = input?parseInt(input, 10):0;      $this.val(function () {         return (input === 0)?"":input.toLocaleString("en-US");      });  });  

Notes:

  • toLocaleString() javascript function Actually show thousands separator (example and doc)
  • run below code in your console to get the idea

    (30000000).toLocaleString('en-US',{useGrouping:true})

like image 28
Iman Avatar answered Sep 20 '22 12:09

Iman