Reputation: 13587
How would I format amount field with comma and decimal?
I am able to get commas but this function does not allow decimal in the field.
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});
Upvotes: 1
Views: 13145
Reputation: 1884
Is this what you had in mind?
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/([0-9])([0-9]{2})$/, '$1.$2')
.replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",")
;
});
});
You can test it here: http://jsfiddle.net/fXrv2/
Upvotes: 4