cuconsortium
asked on
Java Script Format Currency
Dear all,
I'm working with the attached java script function to mask a input text field to "$0.50 or $1,000.50 "
When I run the page, I don't get error message and my input text field does not change to the above currency format either.
I wonder what's going on with script. I'm calling the function as follow:
<body>
<form id="form1" name="form1" method="post" action="">
<input name="money" type="text" id="money" OnBlur="toCurrency(this)"/ >
</form>
</body>
Thank you!!
I'm working with the attached java script function to mask a input text field to "$0.50 or $1,000.50 "
When I run the page, I don't get error message and my input text field does not change to the above currency format either.
I wonder what's going on with script. I'm calling the function as follow:
<body>
<form id="form1" name="form1" method="post" action="">
<input name="money" type="text" id="money" OnBlur="toCurrency(this)"/
</form>
</body>
Thank you!!
function toCurrency(which)
{
var num = which.value;
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)
}
another ref : http://www.w3schools.com/jsref/event_onblur.asp
ASKER
Hi leakim971,
i added which.value = num; at the end of the function. it still doesn't format the input value as currency...
i added which.value = num; at the end of the function. it still doesn't format the input value as currency...
eh I did not notice you return the formatted number at the end.
You can remove which.value = num; (line 8)
and update your input like this :
You can remove which.value = num; (line 8)
and update your input like this :
<input name="money" type="text" id="money" OnBlur="this.value = toCurrency(this)"/>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great~~ it works! But, now I got a bigger problem... I have several of these money fields, and have the other function written for Total Amount field, which will sum up all the money fields. Is there any way to convert the Masked data in money fields back to number, so then, the existing TotalAmount() function will work?
Thank you!
Thank you!
it look like a new question...
to back to a decimal number :
or
When youv'e time check this : http://www.w3schools.com/jsref/jsref_tofixed.asp
to back to a decimal number :
function toDecimal(which) { // $ab.xy
which.value = which.replace(/\D/g, "") / 100;
}
or
function toDecimal(which) { // $ab.xy
return which.replace(/\D/g, "") / 100;
}
When youv'e time check this : http://www.w3schools.com/jsref/jsref_tofixed.asp
ASKER
ok. I'll open a new question.
ASKER
The new question is at:
https://www.experts-exchange.com/questions/27044851/Java-Script-Mask-and-Summing-Up.html
https://www.experts-exchange.com/questions/27044851/Java-Script-Mask-and-Summing-Up.html
Open in new window