I have the following code in Jquery for a forn that I am working on:
$(document).ready(function () {
function RemoveRougeChar(convertString) {
if (convertString.substring(0, 2) == ".") {
return convertString.substring(1, convertString.length)
}
return convertString;
}
$('#float1').on("focus", function (e) {
var $this = $(this);
var num = $this.val().replace(/,/g, "");
$this.val(num);
})
.on("blur", function (e) {
var $this = $(this);
var num = $this.val().replace(/[^0-9]+/g, '').replace(/,/gi, "").split("").reverse().join("");
var num2 = RemoveRougeChar(num.replace(/(.{3})/g, "$1,").split("").reverse().join(""));
$this.val(num2);
});
});
My HTML :
<input id="float1" decimalPlaces="2" type="text" style="width:100px;margin-left:5px;"/>
What this does is for example if I type a value of 4461,65 in the field afer the click the value returned is ,446,165. I would like the outcome to be :4,461.65. I read a lot of solutions here on Stackoverflow regarding this and they all hint to the Jquery formatting plugins. I wish to do it without the plugin so I can understand the code better. Is this possible?
Some times basic javascript is the best solution.