JSFiddle - React, Tailwind, and code Playground
HTML
<form>
<input class="test" type="text" />
</form>
JavaScript
function commaSeparateNumber(index, val) {
val = val.replace(/\D/g, '');
var array = val.split('');
var index = -3;
while (array.length + index > 0) {
array.splice(index, 0, ',');
// Decrement by 4 since we just added another unit to the array.
index -= 4;
}
return array.join('');
};
$(document).on('keyup', '.test', function () {
$(this).val(commaSeparateNumber);
});