JSFiddle - React, Tailwind, and code Playground

by bvotcode

HTML

<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<input type="text" value="123.45" id="number">

JavaScript

var x;

$('#number').on("keyup", function() {
	x = removeCommas(this.value);
  this.value = addCommas(x);
});

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

function removeCommas(value){
  value=value.replace(/\,/g,'');
  value=parseInt(value,10);
  return value;
}