Format Currency

[Total: 0    Average: 0/5]

This example shows how to format integers by inserting commas.

Body Code:

Input integers into each of the following form fields, then click on the form button to
have commas inserted into the numbers.
<SCRIPT LANGUAGE="JavaScript">
function FormatCurrency() {

for (var i = 0; i < document.forms[0].elements.length; i++)
{
if (document.forms[0].elements[i].type=='text')
{  
var textFormat = "";
var t=0;
var str=""; 
for (var j = 0; j < document.forms[0].elements[i].value.length ; j++) 
{
document.forms[0].elements[i].value=document.forms[0].elements[i].value.replace(',','');
}

str=document.forms[0].elements[i].value; 

if (str.length != 0)
{
for (var k = str.length-1; k>=0 ; k--)
{
t++;
if (t % 3 == 0)
{ textFormat = "," + str.substr(k,1) + textFormat; 
} else {
textFormat =  str.substr(k,1) + textFormat;
}
} 

if (textFormat.substr(0,1) == ",")
{ 
document.forms[0].elements[i].value = textFormat.substr(1,textFormat.length-1);
} else { 
document.forms[0].elements[i].value = textFormat;
}
}
}
} 
}
</script>
<form>
   <INPUT type="text" id=text1 name=text1>
   <p></p>
   <INPUT type="text" id=text2 name=text2>
   <p></p>
   <INPUT type="text" id=text3 name=text3>
<P>&nbsp;</P>
<INPUT type="button" value="Button" id=button1 name=button1 onclick="FormatCurrency();">
</form>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *