Hi,
Here is the piece of code that I wrote earlier. Please let me know if
it works as you wished!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<HTML>
<HEAD>
<SCRIPT Language="JavaScript">
function DoRequired (StrName,nValue)
{
//Variables
var txtTextBox = new String(StrName);
var tempString = new String(nValue);
//Perform Numeric Check
if(isNaN(nValue))
{
if (StrName.toString()=="txtPrice")
document.forms(0).elements(0).value="";
else if (StrName.toString()=="txtUnits")
document.forms(0).elements(1).value="";
else if (StrName.toString()=="txtTotal")
document.forms(0).elements(2).value="";
alert("Please enter Numeric value");
}
else
switch (StrName)
{
case "txtPrice" :
formatValue(tempString, 4, 0);
if(document.forms(0).elements(1).value != "")
{
nTotal = document.forms(0).elements(0).value *
document.forms(0).elements(1).value;
nTotal = Math.round(nTotal*100)/100;
document.forms(0).elements(2).value = nTotal;
tempString = new String(nTotal);
formatValue(tempString, 2, 2)
}
else if(document.forms(0).elements(2).value != "")
{
nUnits = document.forms(0).elements(2).value/document.forms(0).elements(0).value;
nUnits = Math.round(nUnits*10000)/10000;
document.forms(0).elements(1).value = nUnits;
tempString = new String(nUnits);
formatValue(tempString, 4, 1)
}
break;
case "txtUnits" :
formatValue(tempString, 4, 1);
if(document.forms(0).elements(0).value != "")
{
nTotal = document.forms(0).elements(0).value *
document.forms(0).elements(1).value;
nTotal = Math.round(nTotal*100)/100;
document.forms(0).elements(2).value = nTotal;
tempString = new String(nTotal);
formatValue(tempString, 2, 2)
}
else if(document.forms(0).elements(2).value != "")
{
nUnitPrice =
document.forms(0).elements(2).value/document.forms(0).elements(1).value;
nUnitPrice= Math.round(nUnitPrice*10000)/10000;
document.forms(0).elements(0).value = nUnitPrice;
tempString = new String(nUnitPrice);
formatValue(tempString, 4, 0)
}
break;
case "txtTotal" :
formatValue(tempString, 2, 2);
if(document.forms(0).elements(0).value != "")
{
nUnits = document.forms(0).elements(2).value/document.forms(0).elements(0).value;
nUnits = Math.round(nUnits*10000)/10000;
document.forms(0).elements(1).value = nUnits;
tempString = new String(nUnits);
formatValue(tempString, 4, 1)
}
else if(document.forms(0).elements(1).value != "")
{
nUnitPrice =
document.forms(0).elements(2).value/document.forms(0).elements(1).value;
nUnitPrice= Math.round(nUnitPrice*10000)/10000;
document.forms(0).elements(0).value = nUnitPrice;
tempString = new String(nUnitPrice);
formatValue(tempString, 4, 0)
}
break;
}
}
//New function : This is reusable function which serves as a
independent serice responsible for formatting the data
// The function accepts the value as entered by user, Number of
digits after the decimal point
// and the index of the elements as a paramenter. It determines
how many zeros are to be padded
// in order to have the required number of digits after decimal
point
function formatValue(tempString, nDecimalDigits, nElementIndex)
{
//Variables
n=""; // variable to hold the decimal part of the entered value
if(tempString.indexOf('.')!=-1) //If Decimal point exists then
{
n=tempString.substr(tempString.indexOf('.')+1); //Extract the
decimal digits of the number
if(n.length <= nDecimalDigits)
{
result = nDecimalDigits - n.length; //calculate how many
zeros are to be padded to the right
for(i=0; i<result; i++)
{
tempString = tempString + "0"; //pad the zeros to the right
}
document.forms(0).elements(nElementIndex).value =
tempString; //set the formatted string in the textbox
}
else
{
if (nElementIndex ==2)
document.forms(0).elements(nElementIndex).value=
Math.round(document.forms(0).elements(nElementIndex).value * 100)/100;
else
document.forms(0).elements(nElementIndex).value=
Math.round(document.forms(0).elements(nElementIndex).value *
10000)/10000;
}
}
else
{
if (nElementIndex!=2)
{document.forms(0).elements(nElementIndex).value=
document.forms(0).elements(nElementIndex).value + ".0000";}
else
{document.forms(0).elements(nElementIndex).value=
document.forms(0).elements(nElementIndex).value + ".00";}
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="GoogleAssign" ACTION="">
<BR>
<BR>
<TABLE BORDER = 0 width="447">
<TR>
<TD width="125"><B> Unit Price</B></TD><TD
width="132"><B>Number of Units</B></TD><TD width="170"><B>
Total Price</B></TD>
</TR>
<TR>
<TD width="125">$<INPUT TYPE="TEXT" NAME="txtPrice" SIZE=10 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
<TD width="132"><INPUT NAME="txtUnits" SIZE=10 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
<TD width="170">$<INPUT NAME="txtTotal" SIZE=15 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML> |