lundi 13 juin 2016

Add comma and a decimal point in a number using jquery [duplicate]


This question already has an answer here:

I used this lines of codes to add comma to the value if changed.

$(document).on('keyup', "input[data-type='number']",function(e){
  if(e.which >= 37 && e.which <= 40){
      e.preventDefault();
  }
  var $this = $(this);
  var num = $this.val().replace(/,/gi, "");
  var num2 = num.split(/(?=(?:d{3})+$)/).join(",");
  console.log(num2);
  // the following line has been simplified. Revision history contains original.
  $this.val(num2);

});

The problem is, if the user input a value that have decimal point like 10500.50, it will remove the comma.

How can I make this compatible with value that has decimal point?


Aucun commentaire:

Enregistrer un commentaire