1

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, ",")
    ;
  });
});
NDM
  • 6,731
  • 3
  • 39
  • 52
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291

1 Answers1

4

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/

Oskar Hane
  • 1,824
  • 13
  • 8
  • @Oskar Hane im following the same answer. But in-appropriate decimal points coming up when i'm using. For example if i want to type 123, it displays as 1.23 – Sandy Jul 11 '15 at 13:33
  • @Himanshu Yadav im following the same answer. But in-appropriate decimal points coming up when i'm using. For example if i want to type 123, it displays as 1.23 – Sandy Jul 11 '15 at 13:33