0

I'm trying to put a comma on a textbox that should only accept numbers. What I did is instead of using type="numbers", I limited the textbox to only accept number keyCodes.

    $('#salary').keydown(function (e) {
        var keyCode = e.which; 
        if (keyCode != 8 && keyCode != 9 && keyCode != 13 && keyCode != 37 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 46 && keyCode != 110 && keyCode != 190) {
            if (keyCode < 48) {
                e.preventDefault();
            } else if (keyCode > 57 && keyCode < 96) {
                e.preventDefault();
            } else if (keyCode > 105) {
                e.preventDefault();
            }
        }
    });

What I want is that after the input is edited(out of focus), the textbox automatically shows commas similar to this value:

1,000,000.00

I am clueless on what to do or use to add comma's on the textbox.

user3942918
  • 25,539
  • 11
  • 55
  • 67
marchemike
  • 3,179
  • 13
  • 53
  • 96
  • Duplicate of https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Phylogenesis Jul 06 '17 at 07:48
  • 1
    Why don't you just filter the Input onkeyup() and format it on blur() with a str.replace()-Regex? – Bernhard Jul 06 '17 at 07:48

4 Answers4

2
$( "#salary" ).blur(function() {
   $( "#salary" ).val( parseFloat($( "#salary" ).val(), 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
});
Vineesh
  • 3,762
  • 20
  • 37
0

Try this solution for add comma in numbers

    $('#salary').keydown(function (){
         var x = $('#salary').val();
         $('#salary').val(addCommas(x));

         function addCommas(x) {
           var parts = x.toString().split(".");
           parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
           return parts.join(".");
        }
});
0

Could you please try with script below. Comma will be remove on click on textbox and thousand seperator will be added after focus out

$( "#salary" ).click(function() {

   $( "#salary" ).val(  $("#salary").val().replace(/,/g, ''));

});



$( "#salary" ).blur(function() {

   $( "#salary" ).val( addCommas($( "#salary" ).val());

});

function addCommas(nStr) {

    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;

}
g.m.ashaduzzaman
  • 2,269
  • 1
  • 14
  • 10
0

for minus and integer only

$("#amount").keypress(function (e) {
var verified = (e.which == 8 || e.which == undefined || e.which == 0) ? null : String.fromCharCode(e.which).match(/[^\-/0-9]/);
if (verified) {
e.preventDefault();
}
});

for integer only

$("#amount").keypress(function (e) {
var verified = (e.which == 8 || e.which == undefined || e.which == 0) ? null : String.fromCharCode(e.which).match(/[^0-9]/);
if (verified) {
e.preventDefault();
}
});

not to display the comma when focus in

$("#amount").focus(function () {
var str = $(this).val();
$(this).val(str.replace(/,/g,''));
});

to display the comma when focus out

$("#amount").focusout(function () {
if ($(this).val().search(",") > 0){
return false;
} else {
if ($(this).val()) {
var add_comma = new Intl.NumberFormat().format($(this).val());
$(this).val(add_comma);}}
});
Diana
  • 21
  • 4