Dear Expert

I have following code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english"> 
<meta http-equiv="Content-Style-Type" content="text/css">

<title>Numeric Textbox Sample</title>

<style type="text/css">
#box1	{width:200px;height:170px;border:1px solid green;background:#e3eeff;padding-top:20px;}

.button	{width:100px;margin-top:0px;}

body {
    margin:0;
margin-top:100px;
	 }
</style>

<script language="javascript">

function NumericDotOnly()
{
var key = window.event.keyCode; 

if (((key >47) && (key <58)) || (key==46))
window.event.returnValue = true;
else 
window.event.returnValue = false;
}

function formatCurrency(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 onload="form2.text1.focus()" >
<center>
<p>I need following format<br>
99,999,999.99<br>
</p>
<div id="box1">
		<form name=form2>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr><td>Amount</td> 

<td><input type=text name=text1 size=15 value="" onkeypress="NumericDotOnly

()";onblur="formatCurrency(this.value)"> </td></tr>	

	<tr><td>Tax</td> <td><input type=text name=text2 size=15 value="" 

onkeypress="NumericDotOnly()";onblur="formatCurrency(this.value)"></td></tr>		
<tr><td>Result</td><td><input type=text name=text3 size=15 value="" disabled></td></tr>

		</table>
		<hr>
		<input class="button" type=button name=command1 value="Plus" 

onclick="form2.text3.value=parseFloat(form2.text1.value)+parseFloat(form2.text2.value)"><br>

		<input class="button" type=button name=command8 value="Focus" 

onclick="form2.text1.select()";"form2.text1.focus()"><br>
		<input class="button" type=reset name=command9 value="Reset">
	
</form>
</div>

</center>
</body>
</html>

Please help me to apply following format

99,999,999.99

Thanks in advance

Finaly I got these codes, these work fine. But there is only problem when I enter data into textboxes second time then but SHOW does not work. How to overcome this problem

<html>
<head>
<script type = "text/javascript">

function addCommas(nStr) {
nStr = nStr.replace(/[^0-9\.]/g,"");
nStr = Number(nStr).toFixed(2);   // remove excess decimals
var rgx = /(\d+)(\d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, '$1,$2');
}
if (nStr.indexOf('.') == -1) {  // if whole number add .00
nStr = nStr + ".00";
}
nStr = nStr.replace(/(\.\d)$/,"$10");  // if only one DP add another 0
return nStr;
}

var x1;
var x2;

function show() {
var totalStr = (x1 + x2).toString();
totalStr = addCommas(totalStr);
document.form1.text3.value = totalStr;
}

</script>

<head>
<body>
<center>
<form name ="form1">
<input type=text name=text1 size=15 value="" onblur="x1 = Number(this.value) || 0; this.value=addCommas(this.value)"><br>
<input type=text name=text2 size=15 value="" onblur="x2 = Number(this.value) || 0; this.value=addCommas(this.value)"><br>
<input type=text name=text3 size=15 value="" disabled><br>
<input type=button value="Show" onclick="setTimeout(show,10)">
</form>
</center>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.