How to add comma as number separator in user input?

If you input is string X then this code below will do it. Just make sure it is in the onKeyPress event.

x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")