Sir,

I have following codes

<!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</title>
<style type="text/css">

body
{
margin-top:220px;
}

#bc
{
    background-color:#FFE4B5;
}

#ft
{
    font-family: "verdana";
    font-size:14;
    font-weight: bolder;
    color: green;
}

.bt
{
    width:100px;
    height:30px;
    background-color:#FFE4E1;
    font-family: "verdana";
    font-size:10;
    font-weight: bolder;
    color: #f75b09;
    margin:3px;
}

.tc
{
    background-color:#ADD8E6;
    font-family: "courier new";
    font-size:15;
    font-weight: bolder;
    color: #red;
    padding:3px;
    width:90px;
    text-align:right;
}

</style>


</style>
<script type = "text/javascript">
//------------------------------------------------------------------
function validate(evt)
{
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var backslsh = key;
var chck = String.fromCharCode( key );

var regex = /[0-9]|\./;
if( !regex.test(chck) ) {
theEvent.returnValue = false;
alert('Please enter Numeric data')
if(theEvent.preventDefault) theEvent.preventDefault();
}
}

//------------------------------------------------------------------
  function total()
  {
    var num1 = form1.text1.value
    var num2 = form1.text2.value
var total = eval(num1) + eval(num2)
form1.text3.value = parseFloat(total).toFixed(2);
  }

//------------------------------------------------------------------

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="form1.text1.focus()">
<center>
<fieldset style=width:300px;background-color:#E0FFFF )>
<Legend style=color:blue;><b><font size=5>Numeric Input</font></b></legend>

<form name="form1">
<table id="ft" cellpadding="1" cellspacing="3" border="0">
<tr>
<td>1st Number</td>
<td><input  class="tc" type="text" name="text1" onkeypress="validate(event)" ; onblur="form1.text1.value=formatCurrency(form1.text1.value)" onfocus="this.select()" >
</td>
</tr>

<tr>
<td>2nd Number</td>
<td><input class="tc" type="text" name="text2" onkeypress="validate(event)" ; onblur="form1.text2.value=formatCurrency(form1.text2.value)" onfocus="this.select()">   
</td>
</tr>

<tr>
<td>Result</td>
<td><input class="tc" type="text" name="text3" disabled>  
</td>
</tr>

<tr>
<td colspan="2"><input class="bt" type="button" name="command1" value="Calculate" onclick="total()"; onfocus="this.select()"><input value="Clear" type="submit" class="bt">    
</td>
</tr>

</table>
</form>
</fieldset>
</center>
</body>
</html>

it shows this result

[IMG]http://i41.tinypic.com/2e2qxw2.jpg[/IMG]

How to get correct result 9,273.47 instead of 1273.47?

Please help

Looks like the issue is that you are trying to sum the values after the formatting change.

You may need to change the logic a bit to store the values and maintain a running total, then use that total, format it and display it in the third input box.

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.