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!
It needs to add x1=x1. replace(\,\g,'') to work correctly when the user changes the number.
You can use type="text" and then add commas to the number in the onchange event.
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.
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.
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
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:
run below code in your console to get the idea
(30000000).toLocaleString('en-US',{useGrouping:true})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With