?
Solved

Cant Get parseInt To Work??

Posted on 2003-03-02
65
Medium Priority
?
610 Views
Last Modified: 2008-03-10
Why wont this code work for me, and always give NaN as my VAT....

function vat(){
var thevat = 0.175;
var total;
var total_one, total_two, total_three, total_four, total_five;
var finalvat;
if(!isNaN(document.beaver_form.cost1.value){
total_one = parseInt(document.beaver_form.cost1.value);
} else  if (!isNaN(document.beaver_form.cost2.value){
total_two = parseInt(document.beaver_form.cost2.value);
} else  if (!isNaN(document.beaver_form.cost3.value){
total_three = parseInt(document.beaver_form.cost3.value);
} else  if (!isNaN(document.beaver_form.cost4.value){
total_four = parseInt(document.beaver_form.cost4.value);
} else  if (!isNaN(document.beaver_form.cost5.value){
total_five = parseInt(document.beaver_form.cost5.value);
}
total = total_one + total_two + total_three + total_four + total_five;
alert(total);
document.beaver_form.vat.value = ('£'+(total)*(thevat)+'.00');
}
0
Comment
Question by:mtcmedia
[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
  • 27
  • 20
  • 13
  • +1
65 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052217
A couple of things--

1)If any of the fields are empty, you will get an error
2) when using parseInt, make sure that you add the radix parameter, or you might get an error:

total_one = parseInt(document.beaver_form.cost1.value,10);

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052230
I thought that my lines of code:

if(!isNaN(document.beaver_form.cost1.value){

would ensure that there was no error if any fields were blank? also what does "radix" the param mean?
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052239
Its ok, I know what the radix is... but how do I get this thing fixed? I though that my use of if/else would stop any errors with empty fields?
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Author Comment

by:mtcmedia
ID: 8052253
Its ok, I know what the radix is... but how do I get this thing fixed? I though that my use of if/else would stop any errors with empty fields?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052269
If your page isn't long, please post it here and I will take a look...

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052288
<HTML><HEAD><TITLE>Crazy Beaver Online Shop</TITLE>

<script language="JavaScript">

//=========================================================
//Update Cost 1 Box

function item_one(){
if (document.beaver_form.item1.checked){
document.beaver_form.cost1.value = ('#'+((document.beaver_form.qi1.value)*(document.beaver_form.pi1.value))+'.00');
} else {
document.beaver_form.cost1.value = ("");
}
vat();
}
 
//End Update Cost 1 Box
//=========================================================

//=========================================================
//Update VAT Box

function vat(){
var thevat = 0.175;
var total;
var total_one, total_two, total_three, total_four, total_five;
var finalvat;

if(!isNaN(document.beaver_form.cost1.value)){
total_one = parseInt(document.beaver_form.cost1.value, 10);
} else  if (!isNaN(document.beaver_form.cost2.value)){
total_two = parseInt(document.beaver_form.cost2.value, 10);
} else  if (!isNaN(document.beaver_form.cost3.value)){
total_three = parseInt(document.beaver_form.cost3.value, 10);
} else  if (!isNaN(document.beaver_form.cost4.value)){
total_four = parseInt(document.beaver_form.cost4.value, 10);
} else  if (!isNaN(document.beaver_form.cost5.value)){
total_five = parseInt(document.beaver_form.cost5.value, 10);
}
total = total_one + total_two + total_three + total_four + total_five;
alert(total);
document.beaver_form.vat.value = ('#'+(total)*(thevat));
}
 
//End Update VAT Box
//=========================================================




</script>      


</head>


<BODY>

<H1>Crazy Beaver Online Shopping</H1>

<P>Welcome to the Crazy Beaver online shop. Suppliers of only the finest knocked
off computer components.</P>

<P>
<HR>

<FORM action="http://www.dcs.st-and.ac.uk/cgi-bin/cgiwrap/stuart/Shop" method="post" name="beaver_form" onSubmit="return validate_form(this)">
<TABLE cellPadding=2 width=786 border=1>
  <TBODY>
  <TR>
    <TD width=18>&nbsp;</TD>
    <TD width=289>Item</TD>
    <TD width=88>Description</TD>
    <TD width=95>MF Code</TD>
    <TD width=92>Price (excl vat)</TD>
    <TD width=51>Quantity</TD>
    <TD width=30>Buy</TD>
    <TD width=55>Cost</TD></TR>
  <TR>
    <TD width=18>1</TD>
    <TD width=289>IBM Deskstar 75GXP 76.8GB UDMA100 <INPUT type=hidden
      value="IBM Deskstar 75GXP &#13;&#10;76.8GB UDMA100" name=di1> </TD>
    <TD width=88>Big Hard Disk</TD>
    <TD width=95>DTLA-307075</TD>
    <TD align=middle width=92>#275.00 <INPUT type=hidden value=275.00
      name=pi1> </TD>
    <TD align=right width=51><INPUT size=3 value=1 name=qi1> </TD>
    <TD align=middle width=30><INPUT type="checkbox" name="item1" onClick="item_one();"> </TD>
    <TD align=middle width=55><INPUT size=7 name=cost1> </TD></TR>
  <TR>
    <TD width=18>2</TD>
    <TD width=289>IBM Deskstar 40GV 41.1Gb UDMA100 <INPUT type=hidden
      value="IBM Deskstar 40GV 41.1Gb &#13;&#10;UDMA100" name=di2> </TD>
    <TD width=88>Big Hard Disk</TD>
    <TD width=95>DTLA-305040</TD>
    <TD align=middle width=92>#102.00 <INPUT type=hidden value=102.00
      name=pi2> </TD>
    <TD align=right width=51><INPUT size=3 value=1 name=qi2> </TD>
    <TD align=middle width=30><INPUT type=checkbox name=item2> </TD>
    <TD align=middle width=55><INPUT size=7 name=cost2> </TD></TR>
  <TR>
    <TD width=18>3</TD>
    <TD width=289>IBM Deskstar 75GXP 30.7Gb UDMA100 <INPUT type=hidden
      value="IBM Deskstar 75GXP &#13;&#10;30.7Gb UDMA100" name=di3> </TD>
    <TD width=88>Big Hard Disk</TD>
    <TD width=95>DTLA-307030</TD>
    <TD align=middle width=92>#104.00 <INPUT type=hidden value=104.00
      name=pi3> </TD>
    <TD align=right width=51><INPUT size=3 value=1 name=qi3> </TD>
    <TD align=middle width=30><INPUT type=checkbox name=item3> </TD>
    <TD align=middle width=55><INPUT size=7 name=cost3> </TD></TR>
  <TR>
    <TD width=18>4</TD>
    <TD width=289>IBM Deskstar 75GXP 46.1Gb UDMA100 <INPUT type=hidden
      value="IBM Deskstar 75GXP &#13;&#10;46.1Gb UDMA100" name=di4> </TD>
    <TD width=88>Big Hard Disk</TD>
    <TD width=95>DTLA-307045</TD>
    <TD align=middle width=92>#117.00 <INPUT type=hidden value=117.00
      name=pi4> </TD>
    <TD align=right width=51><INPUT size=3 value=1 name=qi4> </TD>
    <TD align=middle width=30><INPUT type=checkbox name=item4> </TD>
    <TD align=middle width=55><INPUT size=7 name=cost4> </TD></TR>
  <TR>
    <TD width=18>5</TD>
    <TD width=289>IBM Deskstar 75GXP 61.4Gb UDMA100 <INPUT type=hidden
      value="IBM Deskstar 75GXP &#13;&#10;61.4Gb UDMA100" name=d15> </TD>
    <TD width=88>Big Hard Disk</TD>
    <TD width=95>DTLA-307060</TD>
    <TD align=middle width=92>#199.00 <INPUT type=hidden value=199.00
      name=pi5> </TD>
    <TD align=right width=51><INPUT size=3 value=1 name=qi5> </TD>
    <TD align=middle width=30><INPUT type=checkbox name=item5> </TD>
    <TD align=middle width=55><INPUT size=7 name=cost5> </TD></TR>
  <TR>
    <TD colSpan=7>VAT</TD>
    <TD align=middle width=55><INPUT size=7 name=vat> </TD></TR>
  <TR>
    <TD colSpan=7>Total cost</TD>
    <TD align=middle width=55><INPUT size=7 name=total_cost>
</TD></TR></TBODY></TABLE>
<P></P>
<P>
<HR>

<P></P>
<H2>Payment Details</H2>
<P>
<TABLE cellSpacing=0 cellPadding=2 width=350 border=0>
  <TBODY>
  <TR>
    <TD width=169>Payment Method:</TD>
    <TD width=173><SELECT size=1 name=payment_method> <OPTION value=visa
        selected>Visa</OPTION> <OPTION value=switch>Switch</OPTION> <OPTION
        value=amex>Amex</OPTION></SELECT> </TD></TR>
  <TR>
    <TD width=169>Issue number (switch only)</TD>
    <TD width=173><INPUT size=24 name=issue> </TD></TR>
  <TR>
    <TD width=169>Name of Card Holder:</TD>
    <TD width=173><INPUT size=24 name=card_name> </TD></TR>
  <TR>
    <TD width=169>Card Number:</TD>
    <TD width=173><INPUT size=24 name=card_number> </TD></TR></TBODY></TABLE>
<P></P>
<H2>Shipping Details</H2>
<P>
<TABLE cellSpacing=0 cellPadding=2 width=350 border=0>
  <TBODY>
  <TR>
    <TD width=150>Surname:</TD>
    <TD><INPUT size=24 name=ship_surname></TD></TR>
  <TR>
    <TD width=150>First name:</TD>
    <TD><INPUT size=24 name=ship_firstname></TD></TR>
  <TR>
    <TD width=150>Address:</TD>
    <TD><INPUT size=24 name=ship_address></TD></TR>
  <TR>
    <TD width=150>City/ Town:</TD>
    <TD><INPUT size=24 name=ship_city></TD></TR>
  <TR>
    <TD width=150>Region:</TD>
    <TD><INPUT size=24 name=ship_region></TD></TR>
  <TR>
    <TD width=150>Postcode:</TD>
    <TD><INPUT size=24 name=ship_postcode></TD></TR></TBODY></TABLE>
<P></P>
<P><INPUT type=hidden value=order name=mode><INPUT type=submit value=Checkout>
</FORM></P></BODY></HTML>
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052290
thanks :)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052291
Also, you are missing all of the closing parenthesis on your isNaN statements:

if(!isNaN(document.beaver_form.cost1.value){

should be:

if(!isNaN(document.beaver_form.cost1.value)){

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052308
Yeah I have fixed that as you can see above :) Thanks for this, can you see the problem yet?
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052364
I have also parsed the final total to a Float now, as this will be a decimal number - still NaN! HELPPPPPPP!
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052372
are you sure you want parseInt?

parseFloat will give you a float (or an int if no decimals)

Note that parseFloat doesn't have a radix arg.

BTW, the radix arg in parseInt is 10 by default so it doesn't really matter if you include it for this, as far as I know, no harm in including it though.
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052374
parseFloat all of them ?
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052382
I have also parsed the final total to a Float now, as this will be a decimal number - still NaN! HELPPPPPPP!
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052384
ok I will parseFloat them all....
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052406
the only other thing i can think of is maybe the first char could be a space???

when using parseInt or parseFloat the first char must be a number, so this would fail if any leading spaces.

ok, i just pasted your code and ran it.

THE REASON IS because the first char is NOT a number!  get rid of the dollar sign.. or whatever you call it ;-)

that will do it, i'm sure.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052411
I am just about done, hang on...

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052414
You had me going there! I really though that was it, how stupid of me to do that! BUT, guess what, it still doesnt work! AHHHHHHH!

I really cant see what is wrong :(
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052430
oh ok :)
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8052444
hi
try this at the end of your function
var totstr = total*thevat;
document.beaver_form.vat.value = '#'+totstr +'.00';
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052446
Take a look at this:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
var intTotal = 0;

function setField(strFieldName){
     var objField = document.forms[0].elements[strFieldName]
     if(objField.value.split(" ").join("") ==""){
          objField.value=0;
     }
     if(isNaN(objField.value)){
          alert("Pleae enter a valid number for this field!");
          objField.focus();
          return false;
     }
     intTotal = intTotal +  parseInt(objField.value);
     return true;
}

function calcVat(){
     var thevat = 0.175;
     
     if(!setField('cost1')){
          return false;
     }
     if(!setField('cost2')){
          return false;
     }
     if(!setField('cost3')){
          return false;
     }
     if(!setField('cost4')){
          return false;
     }
     if(!setField('cost5')){
          return false;
     }
     alert(intTotal)
     document.beaver_form.vat.value = ('#'+(intTotal)*(thevat)+'.00');

}
//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052452
Okay, one minor change so that the value resets:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
var intTotal = 0;

function setField(strFieldName){
     var objField = document.forms[0].elements[strFieldName]
     if(objField.value.split(" ").join("") ==""){
          objField.value=0;
     }
     if(isNaN(objField.value)){
          alert("Pleae enter a valid number for this field!");
          objField.focus();
          return false;
     }
     intTotal = intTotal +  parseInt(objField.value);
     return true;
}

function calcVat(){
     var thevat = 0.175;
     intTotal = 0;
     if(!setField('cost1')){
          return false;
     }
     if(!setField('cost2')){
          return false;
     }
     if(!setField('cost3')){
          return false;
     }
     if(!setField('cost4')){
          return false;
     }
     if(!setField('cost5')){
          return false;
     }
     alert(intTotal)
     document.beaver_form.vat.value = ('#'+(intTotal)*(thevat)+'.00');

}
//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052461
jonnal, that doesnt work either...

fritz, you have totally changed my whole program? i was hoping that someone would be able to tell me what was the problem with my code - I dont want to use someone elses whole code! Cant you fix mines? Anyone?
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052464
set these all to 0 and it works

total_one=0, total_two=0, total_three=0, total_four=0, total_five=0;
finalvat=0;
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052466
ah dammit!!!

I just spent fifteen minutes writing something that will fix your problem and you're not interested?

Fritz the Blank
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8052475
ok now I read the question

problem is your else ifs

loose em and sum the total as you go
at the end your function you are adding five var together
some of which might be undefined or empty strings

use this below

========================================================
function vat(){
var thevat = 0.175;
var total;
var finalvat;
if(!isNaN(document.beaver_form.cost1.value)){
total += parseInt(document.beaver_form.cost1.value);
}
if (!isNaN(document.beaver_form.cost2.value)){
total += parseInt(document.beaver_form.cost2.value);
}
if (!isNaN(document.beaver_form.cost3.value)){
total += parseInt(document.beaver_form.cost3.value);
}
if (!isNaN(document.beaver_form.cost4.value)){
total += parseInt(document.beaver_form.cost4.value);
}
if (!isNaN(document.beaver_form.cost5.value)){
total += parseInt(document.beaver_form.cost5.value);
}
var totstr = total*thevat;
document.beaver_form.vat.value = '#'+totstr +'.00';
}

=======================================================

jonnal

0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052481
in your first post of code, take out the dollar sign things, (in VAT too), and add this before the if's to initialize values to 0 so they're a number and it will work.

total_one=0, total_two=0, total_three=0, total_four=0, total_five=0;
finalvat=0;


and you can keep your code
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052488
fritz I am interested, and I thank you very much for your help! But I wanted to fix my code, not have you re write it? Hope you understand?

Thanks dean, working great now..... another question, maybe you can get this one fritz? How can I use the below function for every cost? Rather than having it repeated five times? Is it possible?

function item_one(){
if (document.beaver_form.item1.checked){
document.beaver_form.cost1.value = (((document.beaver_form.qi1.value)*(document.beaver_form.pi1.value))+'.00');
} else {
document.beaver_form.cost1.value = ("");
}
vat();
}
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052492
Here is what is wrong with your code. Consider the following:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkField(strValue){
     if(isNaN(strValue)){
          alert("Please enter a valid number!");
     }

}
//-->
</SCRIPT>

</HEAD>
<BODY>

<FORM action="" method=POST id=form1 name=form1>
<INPUT type="text" id=text1 name=text1>
<INPUT type="button" value="Button" id=button1 name=button1 onClick="checkValue(this.form.text1.value)">
</FORM>
</BODY>
</HTML>


You will see that an empty field will not trigger an alert for the isNaN function. Your code does not guard against empty fields then, and as a result throws an error at the end.

Fritz the Blank
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8052494
oh you might want to set var total;
to 0

var total = 0;
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052506
So, to modify your code, you will need to do something like:

if(!isNaN(document.beaver_form.cost1.value && document.beaver_form.cost1.value!=""){
total_one = parseInt(document.beaver_form.cost1.value);


However, why repeat the code five times when you can encapsulate it into a function like I did above?

In any case, you have the way to fix your code, or another solution that works.

Fritz the Blank
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052526
since you don't want the user to be able to change the price, you should add this to your cost, vat and total inputs that hold values you generate in code.

<INPUT size=7 name=cost2 onFocus=this.blur()>

and you might set the values to 0 as well

<INPUT size=7 name=cost2 onFocus=this.blur() value=0>
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8052528
or

function vat(){
var total=0;
for(i=1;i<6;i++){
var itemx = eval('document.beaver_form.cost'+i+'.value');
if( itemx!="" && !isNaN(itemx))
total += parseInt(itemx);
}
document.beaver_form.vat.value = '#'+(total*0.175) +'.00';
} function vat(){
var total=0;
for(i=1;i<6;i++){
var itemx = eval('document.beaver_form.cost'+i+'.value');
if( itemx!="" && !isNaN(itemx))
total += parseInt(itemx);
}
document.beaver_form.vat.value = '#'+(total*0.175) +'.00';
}
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8052535
sorry double paste there

should be

function vat(){
var total=0;
for(i=1;i<6;i++){
var itemx = eval('document.beaver_form.cost'+i+'.value');
if( itemx!="" && !isNaN(itemx))
total += parseInt(itemx);
}
document.beaver_form.vat.value = '#'+(total*0.175) +'.00';
}
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052555
So here is your code working:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function calcVat(){
var thevat = 0.175;
var intTotal=0;
var total_one=0
var total_two=0;
var total_three = 0;
var total_four = 0;
var total_five = 0;
var finalvat=0;
if(!isNaN(document.beaver_form.cost1.value) && document.beaver_form.cost1.value !=""){
     total_one = parseInt(document.beaver_form.cost1.value);
}
  if (!isNaN(document.beaver_form.cost2.value) && document.beaver_form.cost2.value !=""){
     total_two = parseInt(document.beaver_form.cost2.value);
}
  if (!isNaN(document.beaver_form.cost3.value) && document.beaver_form.cost3.value !=""){
     total_three = parseInt(document.beaver_form.cost3.value);
}
  if (!isNaN(document.beaver_form.cost4.value) && document.beaver_form.cost4.value !=""){
     total_four = parseInt(document.beaver_form.cost4.value);
}
  if (!isNaN(document.beaver_form.cost5.value) && document.beaver_form.cost5.value !=""){
     total_five = parseInt(document.beaver_form.cost5.value);
}
intTotal = parseInt(total_one) + parseInt(total_two) + parseInt(total_three) + parseInt(total_four) + parseInt(total_five);
alert(intTotal);
document.beaver_form.vat.value = ('#'+(intTotal)*(thevat)+'.00');
}
//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052570
Great! Thanks for that, my VAT method is now working! I am now going to try and work out the final price, which is the total, plus VAT.

To do this, I have calculated the total in the VAT method, so I have then called another method, gettotal(total); giving it the total as a param. This works fine, as I have used an alert, and the value is being read into the new method.

However, when I try to add the total to the VAT, I am again getting NaN! I just dont understand why this is happening again? I have made sure I parsed everything to floats, and initialised all variables - look:

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = parseFloat(document.beaver_form.vat.value);
var finaltotal = (n + vatvalue);
document.beaver_form.total_cost.value = ('£'+ finaltotal);
}
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052573
thanks fritz!!! can you help with my new problem?
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052604
thanks fritz!!! can you help with my new problem?
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052607
# what is this for?
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052608
its a £ here, like a dollar sign
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052610
damnit you cant see em, its just like your $
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 200 total points
ID: 8052612
The longest 50 point question ever!!!!

Just so that you know, it is generally considered bad form to piggy back one question on top of another. New questions should go under a new post.

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = 0;
if(document.beaver_form.vat.value != ""){
     vatvalue = document.beaver_form.vat.value;
}
var finaltotal = (n + vatvalue);
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}


Fritz the Blank
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052624
function gettotal(n){
var finaltotal = 0;

// use a different var name than the arg

var num = parseFloat(n);

var vatvalue = parseFloat(document.beaver_form.vat.value);
var finaltotal = (n + vatvalue);
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052625
sorry, ill make sure i start new questions from now on!
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052632
function gettotal(n){
var finaltotal = 0;

// use a different var name than the arg
var num = parseFloat(n);

var vatvalue = parseFloat(document.beaver_form.vat.value);

// oops, forgot to change this one
var finaltotal = (num + vatvalue);

document.beaver_form.total_cost.value = ('#'+ finaltotal);
}
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052634
that above method isnt working yet.... it is taking the "+" as concatenate... not addition.

So say the total is 10, and the VAT 20, its is showing up as 1020?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052647
You can try:

var finaltotal = (parseFloat(n) + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052659
Thats what I tried.... it gives NaN - back to square one   :(

Why is it doing this??
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052668
Thats what I tried.... it gives NaN - back to square one   :(

Why is it doing this??
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052674
function gettotal(n){
var finaltotal = 0;

// use a different var name than the arg
var num = parseFloat(n);
var vatvalue = parseFloat(document.beaver_form.vat.value);

if (isNaN(n)) num = 0;
if (isNaN(vatvalue )) vatvalue = 0;

var finaltotal = (num + vatvalue);

document.beaver_form.total_cost.value = ('#'+ finaltotal);
}

somewhere you must be passing an empty arg or a non-number
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052689
I think I know what the problem is here--you are using a character string in your vat total field,:

('#'+(intTotal)*(thevat)+'.00');
 


and it is the "#" that is causing all of the grief. In order to do what you want, you will need to strip out the #. You can do it like this:

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = 0;
if(document.beaver_form.vat.value != ""){
    vatvalue = document.beaver_form.vat.value.split("#").join("");
}
var finaltotal = (parseFloat(n) + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}


Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052708
Dean, yours is giving me the num - it is not adding the VAT on....

Fritz, yours gives NaN.....
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052709
The saga continues..... I really cant belive this is taking so long to solve
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052743
Is this what you need?

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function calcVat(){
var thevat = 0.175;
var intTotal=0;
var total_one=0
var total_two=0;
var total_three = 0;
var total_four = 0;
var total_five = 0;
var finalvat=0;
if(!isNaN(document.beaver_form.cost1.value) && document.beaver_form.cost1.value !=""){
    total_one = parseInt(document.beaver_form.cost1.value);
}
 if (!isNaN(document.beaver_form.cost2.value) && document.beaver_form.cost2.value !=""){
    total_two = parseInt(document.beaver_form.cost2.value);
}
 if (!isNaN(document.beaver_form.cost3.value) && document.beaver_form.cost3.value !=""){
    total_three = parseInt(document.beaver_form.cost3.value);
}
 if (!isNaN(document.beaver_form.cost4.value) && document.beaver_form.cost4.value !=""){
    total_four = parseInt(document.beaver_form.cost4.value);
}
 if (!isNaN(document.beaver_form.cost5.value) && document.beaver_form.cost5.value !=""){
    total_five = parseInt(document.beaver_form.cost5.value);
}
intTotal = parseInt(total_one) + parseInt(total_two) + parseInt(total_three) + parseInt(total_four) + parseInt(total_five);
alert(intTotal);
document.beaver_form.vat.value = ('#'+(intTotal)*(thevat)+'.00');
gettotal(10)
}

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = 0
     if(document.beaver_form.vat.value !=""){
          vatvalue = document.beaver_form.vat.value.split("#").join("");
     }
var finaltotal = (n + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}

//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
total<INPUT type="text"  name=total_cost><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>

Fritz the Blank
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052756
you need to take out the # like fritz said, this is why you get num with mine and NaN with his.  lose ALL non number chars until the final value you place in the total.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052767
I tested my last bit of code, and it works fine, no errors.

Fritz the Blank
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052768
good luck with your project.  over-and-out
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052770
Close.... but it doesnt work out the total correctly
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052779
what value are you passing as n to your function?

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8052784
i just copied the code you posted
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052788
If I pass intValue, then the total works fine. I wasn't being literal earlier, I was just showing you how to strip out the values. Here is the literal solution:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function calcVat(){
var thevat = 0.175;
var intTotal=0;
var total_one=0
var total_two=0;
var total_three = 0;
var total_four = 0;
var total_five = 0;
var finalvat=0;
if(!isNaN(document.beaver_form.cost1.value) && document.beaver_form.cost1.value !=""){
    total_one = parseInt(document.beaver_form.cost1.value);
}
 if (!isNaN(document.beaver_form.cost2.value) && document.beaver_form.cost2.value !=""){
    total_two = parseInt(document.beaver_form.cost2.value);
}
 if (!isNaN(document.beaver_form.cost3.value) && document.beaver_form.cost3.value !=""){
    total_three = parseInt(document.beaver_form.cost3.value);
}
 if (!isNaN(document.beaver_form.cost4.value) && document.beaver_form.cost4.value !=""){
    total_four = parseInt(document.beaver_form.cost4.value);
}
 if (!isNaN(document.beaver_form.cost5.value) && document.beaver_form.cost5.value !=""){
    total_five = parseInt(document.beaver_form.cost5.value);
}
intTotal = parseInt(total_one) + parseInt(total_two) + parseInt(total_three) + parseInt(total_four) + parseInt(total_five);
alert(intTotal);
document.beaver_form.vat.value = ('#'+(intTotal)*(thevat)+'.00');
gettotal(intTotal)
}

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = 0
     if(document.beaver_form.vat.value !=""){
          vatvalue = document.beaver_form.vat.value.split("#").join("");
     }
var finaltotal = (n + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}

//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
total<INPUT type="text"  name=total_cost><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052798
The trick here is that I passed intTotal to your function rather than an arbitrary number (10) for testing. I should have been clearer.

Fritz the Blank
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8052805
gettotal(intTotal)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052809
And here it is with the dollar sign rather than the pound sign:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function calcVat(){
var thevat = 0.175;
var intTotal=0;
var total_one=0
var total_two=0;
var total_three = 0;
var total_four = 0;
var total_five = 0;
var finalvat=0;
if(!isNaN(document.beaver_form.cost1.value) && document.beaver_form.cost1.value !=""){
    total_one = parseInt(document.beaver_form.cost1.value);
}
 if (!isNaN(document.beaver_form.cost2.value) && document.beaver_form.cost2.value !=""){
    total_two = parseInt(document.beaver_form.cost2.value);
}
 if (!isNaN(document.beaver_form.cost3.value) && document.beaver_form.cost3.value !=""){
    total_three = parseInt(document.beaver_form.cost3.value);
}
 if (!isNaN(document.beaver_form.cost4.value) && document.beaver_form.cost4.value !=""){
    total_four = parseInt(document.beaver_form.cost4.value);
}
 if (!isNaN(document.beaver_form.cost5.value) && document.beaver_form.cost5.value !=""){
    total_five = parseInt(document.beaver_form.cost5.value);
}
intTotal = parseInt(total_one) + parseInt(total_two) + parseInt(total_three) + parseInt(total_four) + parseInt(total_five);
alert(intTotal);
document.beaver_form.vat.value = ('$'+(intTotal)*(thevat)+'.00');
gettotal(intTotal)
}

function gettotal(n){
var finaltotal = 0;
var n = parseFloat(n);
var vatvalue = 0
     if(document.beaver_form.vat.value !=""){
          vatvalue = document.beaver_form.vat.value.split("$").join("");
     }
var finaltotal = (n + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('$'+ finaltotal);
}

//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=beaver_form name=beaver_form>
1<INPUT type="text"  name=cost1><br>
2<INPUT type="text"  name=cost2><br>
3<INPUT type="text"  name=cost3><br>
4<INPUT type="text"  name=cost4><br>
5<INPUT type="text"  name=cost5><br>
vat<INPUT type="text"  name=vat><br>
total<INPUT type="text"  name=total_cost><br>
<a href="#" onClick="calcVat();return false;">Calcultate Vat</a>
</FORM>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8052858
So, are you set now?

Fritz the Blank
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 8053277
Yeah, you are great!

Can you take a look at my other thread?

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20535468.html
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
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

801 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