Link to home
Start Free TrialLog in
Avatar of mtcmedia
mtcmedia

asked on

Cant Get parseInt To Work??

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');
}
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

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
Avatar of mtcmedia
mtcmedia

ASKER

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?
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?
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?
If your page isn't long, please post it here and I will take a look...

Fritz the Blank
<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>
thanks :)
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
Yeah I have fixed that as you can see above :) Thanks for this, can you see the problem yet?
I have also parsed the final total to a Float now, as this will be a decimal number - still NaN! HELPPPPPPP!
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.
parseFloat all of them ?
I have also parsed the final total to a Float now, as this will be a decimal number - still NaN! HELPPPPPPP!
ok I will parseFloat them all....
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.
I am just about done, hang on...

Fritz the Blank
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 :(
oh ok :)
hi
try this at the end of your function
var totstr = total*thevat;
document.beaver_form.vat.value = '#'+totstr +'.00';
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
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
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?
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;
ah dammit!!!

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

Fritz the Blank
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

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
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();
}
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
oh you might want to set var total;
to 0

var total = 0;
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
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>
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';
}
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';
}
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
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);
}
thanks fritz!!! can you help with my new problem?
thanks fritz!!! can you help with my new problem?
# what is this for?
its a £ here, like a dollar sign
damnit you cant see em, its just like your $
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
}
sorry, ill make sure i start new questions from now on!
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);
}
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?
You can try:

var finaltotal = (parseFloat(n) + parseFloat(vatvalue));
document.beaver_form.total_cost.value = ('#'+ finaltotal);
}
Thats what I tried.... it gives NaN - back to square one   :(

Why is it doing this??
Thats what I tried.... it gives NaN - back to square one   :(

Why is it doing this??
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
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
Dean, yours is giving me the num - it is not adding the VAT on....

Fritz, yours gives NaN.....
The saga continues..... I really cant belive this is taking so long to solve
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
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.
I tested my last bit of code, and it works fine, no errors.

Fritz the Blank
good luck with your project.  over-and-out
Close.... but it doesnt work out the total correctly
what value are you passing as n to your function?

Fritz the Blank
i just copied the code you posted
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
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
gettotal(intTotal)
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
So, are you set now?

Fritz the Blank
Yeah, you are great!

Can you take a look at my other thread?

https://www.experts-exchange.com/questions/20535468/Cant-Get-parseInt-To-Work.html