cuconsortium
asked on
Java Script Mask and Summing Up
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!!
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>
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)
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the points!
ASKER
Hi Leakim971,
Unfortunately, I found a bug....
when I add up $1.00 + $1.00 + $1.50 + $0.75 , The total returns me $4.11
It should be $6.05
will you be able to fix it?
Thank you!
Unfortunately, I found a bug....
when I add up $1.00 + $1.00 + $1.50 + $0.75 , The total returns me $4.11
It should be $6.05
will you be able to fix it?
Thank you!
You mean $4.25 (and not $6.05)
(String.prototype.cleanNum ber corrected)
(String.prototype.cleanNum
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
String.prototype.cleanNumber = function() {
var x = 1;
var e = this.indexOf(".");
if(e>=0) {
x = Math.pow(10, this.length-e-1);
}
var n = this;
n = n.replace(/\D/g,"");
var d = n / x;
if(isNaN(d)) d=0;
return d;
}
function TotalCC() {
var l=document.form1.txt_money1.value.cleanNumber();
var a=document.form1.txt_money2.value.cleanNumber();
var b=document.form1.txt_money3.value.cleanNumber();
var c=document.form1.txt_money4.value.cleanNumber();
var d=document.form1.txt_money5.value.cleanNumber();
var e=document.form1.txt_money6.value.cleanNumber();
var f=document.form1.txt_money7.value.cleanNumber();
var g=document.form1.txt_money8.value.cleanNumber();
var h=document.form1.txt_money9.value.cleanNumber();
var i=document.form1.txt_money10.value.cleanNumber();
var j=document.form1.txt_money11.value.cleanNumber();
var k=document.form1.txt_money12.value.cleanNumber();
var totalCC;
totalCC = l + a + b + c + d + e + f + g + h + i + j + k;
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)
}
</script>
</head>
<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" disabled="disabled"/>
</form>
</body>
</html>
ASKER
yup, $4.25.
It looks right now! Thanks~! What was wrong with it?
It looks right now! Thanks~! What was wrong with it?
parseInt(this.replace(/\D/ g,""))/x return a wrong number
ASKER
Yah, I was wondering about the parseInt at the first glance. However, Thank you!
You're welcome!
Here's a more simple version:
Open in new window