How can I format the text entered into an HTML textfield like currency?
css, html, javascript
Solution
First result in a Google search for "javascript format currency"
http://www.web-source.net/web_development/currency_formatting.htm
function CurrencyFormatted(amount)
{
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
Problem
I have a series of texfields, which I'd like to format as currency. Preferably, this would be done on the fly but at least onblur. What I mean by currency format is 349507 -> $349,507. Is it possible? I prefer HTML/CSS/JS solutions, because I need less explanation. I'm not familiar at all with jQuery. Any help is greatly appreciated. Mike