Click to See Complete Forum and Search --> : Whats Wrong With This??? parseInt
mtcmedia
03-02-2003, 10:07 AM
function vat(){
var thevat;
var finalvat;
thevat = 0.175
document.beaver_form.vat.value = ('£'+(parseInt((document.beaver_form.cost1.value))+parseInt((document.beaver_form.cost2.value))+pars eInt((document.beaver_form.cost3.value))+parseInt((document.beaver_form.cost4.value))+parseInt((docu ment.beaver_form.cost5.value)))*(thevat)+'.00');
}
why is this giving me NaN?
khalidali63
03-02-2003, 10:36 AM
Looks like one of the values from the text fields is not an integer value..
Validate the inputs from all the text fields first.
Cheers
Khalid
mtcmedia
03-02-2003, 10:42 AM
Have changed it to this, but still giving NaN? Please help again! Thanks!!!
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');
}
mtcmedia
03-02-2003, 10:51 AM
Fixed the parenthesis..... still giving NaN, HELP!!!!
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));
}
khalidali63
03-02-2003, 12:32 PM
Well ..here you go I hope this reduces the misery....:D
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"></meta>
<meta name="Author" content="Khalid Ali"></meta>
<title>Untitled</title>
<link type="text/css" rel="stylesheet" href="http://68.145.35.86/skills/javascripts/style/js_tutorials_style.css">
<style type="text/css">
</style>
<script type="text/javascript">
function process(){
var frmObj = document.beaver_form.cost;
var thevat = 0.175;
var total=0;
var total_one, total_two, total_three, total_four, total_five;
for(x=0;x<frmObj.length;x++){
var val = frmObj[x].value;
if(val!=""){
if(!isNaN(parseInt(val))){
total+= parseInt(val);
}else{
alert("Text field does not have valid integer value.")
frmObj[x].focus();
return false;
}
}else{
//you can add here an alert box for field that must not be empty
}
}
document.beaver_form.vat.value = ((total)*(thevat)).toFixed(2);
}
</script>
</head>
<body>
<form name="beaver_form" action="">
<input type="Text" name="cost"></input><br>
<input type="Text" name="cost"></input><br>
<input type="Text" name="cost"></input><br>
<input type="Text" name="cost"></input><br>
<input type="Text" name="cost"></input><br>
£<input type="Text" name="vat"></input><br>
<input type="Button" value = "Process Form" onclick="process()"></input><br>
</form>
</body>
</html>
Cheers
Khalid
Nedals
03-02-2003, 01:25 PM
In your first post, as khalidali63 noted, it is probable that one of the <input... >'s contained nothing. In your second post the 'else if' will ensure that one of your total_xxx will not be set. Both will result in an NaN
khalidali63's second post correctly checks and ignores any null value. This code also uses a loop to process the input. Whenever you find yourself writing repetitive code, look for an opportunity to use a loop.
Hope this helps you understand what's happening with your code.
khalidali63
03-03-2003, 10:03 AM
Did it work for you?
Khalid
mtcmedia
03-03-2003, 10:30 AM
yeah, thanks! I managed to fix it, im on my second problem now:
http://forums.webdeveloper.com/showthread.php?s=&threadid=5214