Reputation: 379
I am trying to add commas when the user type a number. What is the correct syntax to select the input form-control
class with attribute number
type in Jquery?
EDIT: I can't change the HTML code since it is outputted from Django with Bootstrap.
HTML
<span class="input-group-addon">$</span>
<input class="form-control" id="id_step2-number_2"
name="step2-number_2" type="number">
<span class="input-group-addon">.00</span>
JQuery
$(document).ready(function(){
$("input[type='number']").keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "")
.split("").reverse().join("");
var num2 = RemoveRougeChar(
num.replace(/(.{3})/g,"$1,").split("")
.reverse().join("")
);
console.log(num2);
// the following line has been simplified.
// Revision history contains original.
$this.val(num2);
});
});
function RemoveRougeChar(convertString){
if(convertString.substring(0,1) == ","){
return convertString.substring(1, convertString.length)
}
return convertString;
}
JSFiddle Link https://jsfiddle.net/yWTLk/767/
Upvotes: 14
Views: 49540
Reputation: 3427
As many people are not using jQuery nowadays, here is a simple example with vanilla javascript.
You might have different use cases to add comma-separated values for numbers. Here is a complete guide to using commas as thousand separated using javascript.
let inputElement = document.querySelector("#number_element");
inputElement.addEventListener("keyup",(event)=>{
var tempNumber = inputElement.value.replace(/,/gi, "");
var commaSeparatedNumber = tempNumber.split(/(?=(?:\d{3})+$)/).join(",");
inputElement.value = commaSeparatedNumber;
})
<input id="number_element" type="text">
<span>.00</span>
Upvotes: 0
Reputation: 9476
Change type="number"
to type="text"
because you can't put comma into number.
<input class="form-control" id="id_step2-number_2"
name="step2-number_2" type="text">
Check updated Fiddle
Upvotes: 5
Reputation: 13848
First of all, you can't use input type=number
, because it won't allow the character ,
which you are using to notate number group.
Second, with a good regular expression, this is actually super easy, and really no need for reverse and reverse, which is boring.
var num = $this.val().replace(/,/gi, "");
var num2 = num.split(/(?=(?:\d{3})+$)/).join(",");
The first line removes commas from input value.
The second line is magic, it splits the value into groups of 3 from last to first, then join with comma. For example, '1234567'
will become ['1', '234', '567']
.
I use split
here is just to show the power of RegExp, in practice we don't need to split and join, just replace directly:
var num2 = num.replace(/\d(?=(?:\d{3})+$)/g, '$&,');
But, the real magic is, I bet you would yell, toLocaleString
! What?!
(1234567).toLocaleString() // "1,234,567"
Upvotes: 22
Reputation: 902
Using the input type "number" seems to prevent inserting commas into the input field with Javascript. If you'd like to insert commas, you may want to use a "text" input instead, but this will prevent numeric validation on browsers with Javascript disabled.
The fun will never end, it's HTML5.
Upvotes: 8