Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Further problems with totaling script

Posted on 2001-07-31
7
226 Views
Last Modified: 2012-06-22
I have had help with this one before but now a new problem was discovered with it.  If in the first
price field, you enter a $, it does not total as it should and causes errors.  It doesn't work and causes errors when you use the auto-complete feature
that is built into IE to fill in the quantity field.  Here is the 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.Request.totalA.value = dp((document.Request.Qty_A.value)*(document.Request.Price_A.value))
document.Request.totalB.value = dp((document.Request.Qty_B.value)*(document.Request.Price_B.value))
document.Request.totalC.value = dp((document.Request.Qty_C.value)*(document.Request.Price_C.value))
document.Request.totalD.value = dp((document.Request.Qty_D.value)*(document.Request.Price_D.value))
document.Request.totalE.value = dp((document.Request.Qty_E.value)*(document.Request.Price_E.value))
document.Request.totalF.value = dp((document.Request.Qty_F.value)*(document.Request.Price_F.value))
document.Request.totalG.value = dp((document.Request.Qty_G.value)*(document.Request.Price_G.value))
document.Request.totalH.value = dp((document.Request.Qty_H.value)*(document.Request.Price_H.value))
document.Request.Total_Value.value = dp(eval(document.Request.totalA.value) + eval(document.Request.totalB.value)
+ eval(document.Request.totalC.value) + eval(document.Request.totalD.value) + eval(document.Request.totalE.value)
+ eval(document.Request.totalF.value) + eval(document.Request.totalG.value) + eval(document.Request.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()
}*/
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=ele.value.replace(re,'');
calculate();
}
</SCRIPT>

Here is an example of a line item:

<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 value="<%=Session("Item_B")%>"></p></TD>
<TD align=middle><p align="center"><INPUT size=2 name=Qty_B onChange="calculate()" value="<%=Session("QTY_B")%>"></p></TD>
<TD align=middle><p align="center"><INPUT size=18 name=Description_B value="<%=Session("Description_B")%>"></p></TD>
<TD align=middle><p align="center"><INPUT size=10 name=Price_B onChange="regExp(this)" value="<%=Session("Price_B")%>"><FONT
face="arial, helvetica" size=2>EA.</FONT></p></TD>
</TR>

The toaling is done with hidden fields:

<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">

Any ideas what is wrong with it to cause it to do this?  Any help is greatly appreciated.
0
Comment
Question by:beegled
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:coreyit
ID: 6339581
I'm sure there are better ways, but a quick one to remove the dollar signs just before calculating is to add

---code---
.replace( '$', '' )
---code---

to the end of each

---code---
document.Request.Price_XXX.value
---code---

line so you have

---code---
document.Request.Price_XXX.value.replace( '$', '' )
---code---


-corey
0
 
LVL 2

Expert Comment

by:coreyit
ID: 6339584
note that those are all single quotes.

-corey
0
 
LVL 1

Accepted Solution

by:
pjtt earned 200 total points
ID: 6341164
Or you could get rid of both "$" and "," with:

document.Request.Price_XXX.value.replace( /[$,]/g, '' )

By the way, you could consolidate your code like this:

var aSomeLetters = ["A", ""B, "C", "D", "E", "F",
"G", "H"]
function calculate() {
for (var i=0; i<aSomeLetters.length && document.Request.elements["total" + aSomeLetters[i]]; i++) {
document.Request.elements["total" + aSomeLetters[i]].value = dp(document.Request.elements["Qty_" + aSomeLetters[i]].value * document.Request.elements["Price_" + aSomeLetters[i]].value)
document.Request.Total_Value.value += document.Request.elements["total" + aSomeLetters[i]].value * 1
}
}


Peter Tracey
Developer
http://www.25online.com/peter
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:beegled
ID: 6343144
Any ideas on why the script does not work when somebody fills in the fields using the auto-complete feature that is built into IE?  Thanks so much for help by the way.
0
 

Author Comment

by:beegled
ID: 6357658
Wish I culd have solved the whole problem but this was great.  Thanks!
0
 
LVL 1

Expert Comment

by:pjtt
ID: 6357690
If you want to solve it if they use the autocomplete feature you'll need to add an onsubmit function that goes through the form after it's all filled out.

Peter Tracey
Developer
0
 

Author Comment

by:beegled
ID: 6361185
Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question