Dear all,
I have 12 text fields, which need to be masked to currency format, and one text field, which is adding up these 12 text fields and display the Total as currency format.
My TotalCC() Java Script function was working fine until I apply my Format(num) Java Script function to mask the 12 money fields into this format: $0.50 or $1,000,000.50
When I only entered the 1st money field, the Total field adds up the amount correctly, but displays the number without the mask. I then go ahead entered the 2nd money field, the Total field becomes "NaN"
How do I make it work so that the Total field will sum up correctly and display with my currency mask?
I'm attaching the functions and the HTML codes as follow.
Thank you!!
<body>
<form id="form1" name="form1" method="post" action="">
<input name="txt_money1" type="text" id="txt_money1" onblur="this.value=format(this.value)" onchange="TotalCC()" />
<br>
<input name="txt_money2" type="text" id="txt_money2" onblur="this.value=format(this.value)" onchange="TotalCC()" />
<br>
<input name="txt_money3" type="text" id="txt_money3" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money4" type="text" id="txt_money4" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money5" type="text" id="txt_money5" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money6" type="text" id="txt_money6" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<br>
<input name="txt_money7" type="text" id="txt_money7" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money8" type="text" id="txt_money8" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money9" type="text" id="txt_money9" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money10" type="text" id="txt_money10" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money11" type="text" id="txt_money11" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
<input name="txt_money12" type="text" id="txt_money12" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<br>
Total:
<input name="txt_totalCC" type="text" id="txt_totalCC" onchange="TotalCC();this.value=format(this.value)"/>
<br>
</form>
</body>
Select all Open in new window
function TotalCC()
{
var l=document.form1.txt_money1.value;
var a=document.form1.txt_money2.value;
var b=document.form1.txt_money3.value;
var c=document.form1.txt_money4.value;
var d=document.form1.txt_money5.value;
var e=document.form1.txt_money6.value;
var f=document.form1.txt_money7.value;
var g=document.form1.txt_money8.value;
var h=document.form1.txt_money9.value;
var i=document.form1.txt_money10.value;
var j=document.form1.txt_money11.value;
var k=document.form1.txt_money12.value;
var totalCC;
totalCC = (l*1) + (a*1) + (b*1) + (c*1) + (d*1) + (e*1)+ (f*1)+ (g*1)+ (h*1)+ (i*1)+ (j*1)+ (k*1)
document.form1.txt_totalCC.value = totalCC
}
function format(num)
{
num = num.toString().replace(/\$|\,/g, '');
if (isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 + 0.50000000001);
cents = num % 100;
num = Math.floor(num / 100).toString();
if (cents < 10) cents = '0' + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3);
i++) { num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring (num.length - (4 * i + 3)) }
return (((sign) ? '' : '-') + '$' + num + '.' + cents)
}
Select all Open in new window
Here's a more simple version:
Open in new window