When I click on the cost fields when they are blank and then click out of the $0.00 displays. I want the fields to remain blank if the user does not enter any value. Can someone help me out how do I achieve that? Listed below is the code that I have:
var numberWithCommas = function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var removeCommas = function(x) {
return x.replace(/,/g, '');
};
var removeDollarSign = function(x) {
return x.indexOf('$') === 0 ? x.substr(1) : x;
};
var toNumber = function(num) {
return isNaN(num) || num === '' || num === null ? 0.00 : num;
};
var toValue = function(str) {
return toNumber(removeDollarSign(removeCommas(str)));
};
var formatCurrency = function(val) {
return '$' + numberWithCommas(parseFloat(toValue(val)).toFixed(2));
};
jQ('#costWages, #costExpenses').blur(function(event) {
event.target.value = formatCurrency(event.target.value);
});
toNumber, just replace0.00with""?!numberWithCommas