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-num ber+3);
return string;
}
function calculate()
{
document.Order.totalA.valu e = dp((document.Order.Qty_A.v alue)*(doc ument.Orde r.Price_A. value))
document.Order.totalB.valu e = dp((document.Order.Qty_B.v alue)*(doc ument.Orde r.Price_B. value))
document.Order.totalC.valu e = dp((document.Order.Qty_C.v alue)*(doc ument.Orde r.Price_C. value))
document.Order.totalD.valu e = dp((document.Order.Qty_D.v alue)*(doc ument.Orde r.Price_D. value))
document.Order.totalE.valu e = dp((document.Order.Qty_E.v alue)*(doc ument.Orde r.Price_E. value))
document.Order.totalF.valu e = dp((document.Order.Qty_F.v alue)*(doc ument.Orde r.Price_F. value))
document.Order.totalG.valu e = dp((document.Order.Qty_G.v alue)*(doc ument.Orde r.Price_G. value))
document.Order.totalH.valu e = dp((document.Order.Qty_H.v alue)*(doc ument.Orde r.Price_H. value))
document.Order.Total_Value .value = dp(eval(document.Order.tot alA.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.sub str(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></T D>
<TD align=middle><p align="center"><INPUT size=10 name=Price_B onChange="regExp(this)"><F ONT face="arial, helvetica" size=2>EA.</FONT></p></TD>
</TR>
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.
return string;
}
function calculate()
{
document.Order.totalA.valu
document.Order.totalB.valu
document.Order.totalC.valu
document.Order.totalD.valu
document.Order.totalE.valu
document.Order.totalF.valu
document.Order.totalG.valu
document.Order.totalH.valu
document.Order.Total_Value
}
function regExp(ele)
{
d=ele.value.indexOf('.')
dot=/\./g
e=ele.value.substr(0,d+1)
if(d!=-1){e+=ele.value.sub
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 align=middle><p align="center"><INPUT size=18 name=Description_B></p></T
<TD align=middle><p align="center"><INPUT size=10 name=Price_B onChange="regExp(this)"><F
</TR>
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();
}
{
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(
}
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).re place(/^\. /,'0.')
}
function regExp(ele)
{
ele.value = parseFloat(ele.value)
}
which is equivalent to
function regExp(ele)
{
ele.value = ele.value.replace(/^(\d*(\
}
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
}
sorry, if you DON'T want the leading zero, use
function dp(price)
{
return String(0.01*Math.round(100 *price).re place(/^0* /,'')
}
function dp(price)
{
return String(0.01*Math.round(100
}
or use this script
var validCharacters = "0123456789.";
function validString(val)
{
var sTmp = "";
for(i=0;i<val.length;i++)
if(validCharacters.indexOf (val.charA t(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.673234 5AA");
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.charA t(i))>=0)
{
if(val.charAt(i)!="." || (val.charAt(i)=="." && (val.indexOf(".")!=val.las tIndexOf(" .") && i!=val.lastIndexOf(".")))
sTmp += val.charAt(i);
}
}
return sTmp;
}
var validCharacters = "0123456789.";
function validString(val)
{
var sTmp = "";
for(i=0;i<val.length;i++)
if(validCharacters.indexOf
return sTmp;
}
add any characters that should remain in the variable in the validCharacters string. And use it like:
var myString = validString("123456.673234
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
{
if(val.charAt(i)!="." || (val.charAt(i)=="." && (val.indexOf(".")!=val.las
sTmp += val.charAt(i);
}
}
return sTmp;
}
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.?
Is there any way to allow people to just enter a 2 rather than 2.?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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