0

I'm trying to display properly formatted numbers in my autoForm input fields. The code below prints, as expected, to the console when I use a console.log statement. However, when returned to the client, 500000 displays 5,0,0,000 and .toLocaleString() doesn't work at all in the display or console. Any ideas?

Template.mortgage.events({
'keyup [name=cashOut]': function(){
  var cashOut = event.target.value,
  formatted = cashOut.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
  event.target.value = formatted;
}
});
Mike
  • 1,180
  • 3
  • 15
  • 28
  • Is it applying the regex twice? – CodeChimp Jul 29 '16 at 17:08
  • CodeChimp, thanks! I think you're right, but I'm lost on how to stop it from repeating as I both grab and return the value from event.target.value so it looks recursive. How would you do it? – Mike Jul 29 '16 at 19:12

1 Answers1

0

The specific problem you're having is the use of the g modifier on your regex. I get what you're trying to do - comma-ify the value to make it more readable. A better regex to do what you want can be found in the accepted answer for this question.

Community
  • 1
  • 1
Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39