JSFiddle - React, Tailwind, and code Playground
by dying_sakura
HTML
<span class="input-group-addon">$</span>
<input class="form-control" id="id_step2-number_2" name="step2-number_2" type="text">
<span class="input-group-addon">.00</span>
JavaScript
$(document).ready(function(){
$("#id_step2-number_2").keyup(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "").split("").reverse().join("");
var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));
console.log(num2);
// the following line has been simplified. Revision history contains original.
$this.val(num2);
});
});
function RemoveRougeChar(convertString){
if(convertString.substring(0,1) == ","){
return convertString.substring(1, convertString.length)
}
return convertString;
}