Link to home
Start Free TrialLog in
Avatar of beegled
beegled

asked on

Problem with totaling script

I recently posted a script that I had that was adding certain fields to come up with a total only the problem was that it would not work if somebody added in any characters that weren't numbers.  Somebody was very gracious in helping me fix that.  Another problem has come up with the script in that if a number is entered for the price that does not include at least a decimal point at the end, the script actually deletes what the entered into the price field.  Does anybody know of a work around for this?

Here is the script:

<SCRIPT LANGUAGE="JavaScript">
function LoadFuncs() {
if (document.js) initNav();
calculate}
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function dp(price)
{
string = "" + price;
number = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (number == 1)
return string + '00';
if (number == 2)
return string + '0';
if (number > 3)
return string.substring(0,string.length-number+3);
return string;
}
function calculate()
{
document.Order.totalA.value = dp((document.Order.Qty_A.value)*(document.Order.Price_A.value))
document.Order.totalB.value = dp((document.Order.Qty_B.value)*(document.Order.Price_B.value))
document.Order.totalC.value = dp((document.Order.Qty_C.value)*(document.Order.Price_C.value))
document.Order.totalD.value = dp((document.Order.Qty_D.value)*(document.Order.Price_D.value))
document.Order.totalE.value = dp((document.Order.Qty_E.value)*(document.Order.Price_E.value))
document.Order.totalF.value = dp((document.Order.Qty_F.value)*(document.Order.Price_F.value))
document.Order.totalG.value = dp((document.Order.Qty_G.value)*(document.Order.Price_G.value))
document.Order.totalH.value = dp((document.Order.Qty_H.value)*(document.Order.Price_H.value))
document.Order.Total_Value.value = dp(eval(document.Order.totalA.value) + eval(document.Order.totalB.value) + eval(document.Order.totalC.value) + eval(document.Order.totalD.value) + eval(document.Order.totalE.value) + eval(document.Order.totalF.value) + eval(document.Order.totalG.value) + eval(document.Order.totalH.value))
}
function regExp(ele)
{
d=ele.value.indexOf('.')
dot=/\./g
e=ele.value.substr(0,d+1)
if(d!=-1){e+=ele.value.substr(d+1, ele.value.length).replace(dot,'')}
re=/[^0-9.]+/g
ele.value=e.replace(re,'')
calculate()
}
</SCRIPT>

I use hidden fields for the line totals:

<input type="hidden" name="totalA">
<input type="hidden" name="totalB">
<input type="hidden" name="totalC">
<input type="hidden" name="totalD">
<input type="hidden" name="totalE">
<input type="hidden" name="totalF">
<input type="hidden" name="totalG">
<input type="hidden" name="totalH">

Here is what one of the lines looks like:

<TR>
<TD><p align="left"><FONT face="arial, helvetica" size=2>B)</FONT></p></TD>
<TD align=middle><p align="center"><INPUT size=12 name=Item_B></p></TD>
<TD align=middle><p align="center"><INPUT size=2 name=Qty_B onChange="calculate()"></p></TD>
<TD align=middle><p align="center"><INPUT size=18 name=Description_B></p></TD>
<TD align=middle><p align="center"><INPUT size=10 name=Price_B onChange="regExp(this)"><FONT face="arial, helvetica" size=2>EA.</FONT></p></TD>
</TR>
Avatar of TTom
TTom

Sounds to me like you could do something like:

if (pricefield.length > 0 && pricefield.indexOf(".") == -1) {pricefield = pricefield + "."}

However, you might also be able to use parseFloat() against your price field to turn it into a number.

I am not a real RegExp guru, so I am not sure quite how to integrate this with your regExp() function.

Tom
Can you post your full code, so that we can trace it better?

:-)
function regExp(ele)
{
d=ele.value.indexOf('.');
dot=/\./g;

if(d!=-1){
  e=ele.value.substr(0,d+1);
  e+=ele.value.substr(d+1, ele.value.length).replace(dot,'');
}
re=/[^0-9.]+/g;
ele.value=e.replace(re,'');
calculate();
}
make it simple:

function regExp(ele)
{
  ele.value = parseFloat(ele.value)
}
which is equivalent to

function regExp(ele)
{
  ele.value = ele.value.replace(/^(\d*(\.\d*){0,1}).*/,'$1')
}

as for the dp - use

function dp(price)
{
  return 0.01*Math.round(100*price)
}

Unlike the original code, it returns "0.24" for ".2378"; if you do not want the leading zero, use

function dp(price)
{
  return String(0.01*Math.round(100*price).replace(/^\./,'0.')
}
sorry, if you DON'T want the leading zero, use

function dp(price)
{
 return String(0.01*Math.round(100*price).replace(/^0*/,'')
}
or use this script

var validCharacters = "0123456789.";
function validString(val)
{
   var sTmp = "";
   for(i=0;i<val.length;i++)
      if(validCharacters.indexOf(val.charAt(i))>=0) sTmp += val.charAt(i);
   return sTmp;  
}


add any characters that should remain in the variable in the validCharacters string. And use it like:
var myString = validString("123456.6732345AA");

it won't do anything to stop the user from enter a second point though!! If you also want that we will need to know what to do when a second decimal point has been entered. If you need to delete that last decimal point you can do:

var validCharacters = "0123456789.";
function validString(val)
{
   var sTmp = "";
   for(i=0;i<val.length;i++)
   {
      if(validCharacters.indexOf(val.charAt(i))>=0)
      {
         if(val.charAt(i)!="." || (val.charAt(i)=="." && (val.indexOf(".")!=val.lastIndexOf(".") && i!=val.lastIndexOf(".")))
           sTmp += val.charAt(i);
      }
   }
   return sTmp;  
}
Avatar of beegled

ASKER

Okay, again I have done a terrible job of explaining the problem I was running into although it seems that perhaps your folks have found other problems?  Anyway, the problem I was having was that when you enter a price of for example $2.00, you had to at least enter 2. and could not just enter 2

Is there any way to allow people to just enter a 2 rather than 2.?
ASKER CERTIFIED SOLUTION
Avatar of ahosang
ahosang
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beegled

ASKER

Yes, that's works quite beautifully.  Thank you so much for the help.  Thank you to al of you.  I really appreciate you all being so nice to a newbie.