Solved

Problem with totaling script

Posted on 2001-07-06
9
177 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Tutorial help 2 51
PDF Turn Look 7 37
Date Picker is picky (about something) 4 42
CSS Selector Chain NOT working for this situation? 3 16
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

752 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