Solved

Problem with totaling script

Posted on 2001-07-06
9
179 Views
Last Modified: 2012-05-04
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>
0
Comment
Question by:beegled
[X]
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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 9

Expert Comment

by:TTom
ID: 6260855
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
0
 
LVL 7

Expert Comment

by:daniel_c
ID: 6260983
Can you post your full code, so that we can trace it better?

:-)
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6261099
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();
}
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 11

Expert Comment

by:alexcohn
ID: 6262339
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.')
}
0
 
LVL 11

Expert Comment

by:alexcohn
ID: 6262342
sorry, if you DON'T want the leading zero, use

function dp(price)
{
 return String(0.01*Math.round(100*price).replace(/^0*/,'')
}
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6262370
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;  
}
0
 

Author Comment

by:beegled
ID: 6275118
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.?
0
 
LVL 12

Accepted Solution

by:
ahosang earned 200 total points
ID: 6275170
I made an error in the regExp function:
Try this instead:

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();
}
0
 

Author Comment

by:beegled
ID: 6275201
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.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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 …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

627 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