Topic awaiting preservation: Calculation Null Value Problem! (Page 1 of 1)

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted 08-11-2003 01:27

Hi, guys. Can I ask this as well?
I made this script last week. I was pretty happy about it until I found horrible error about null value entity.

My exercise was:

quote:
User can buy three different types of tickets. Standard is \$50. Clsoe up is \$70. And back stage is \$250. User can choose ticket type through dropdown box.

Problem is:

It works only when user put number of ticket into *all* three boxes. Otherwise total box shows just Null!!! I don't know why this happens.....Could you give me tips, please???

Here is my code:

code:
```<html>
<script type="text/javascript">
function cal(){

if(parseInt(form1.sta.value)<0){

return false;

}

if(parseInt(form1.clo.value)<0){

return false;

}

if(parseInt(form1.bac.value)<0){

return false;

}

switch(form1.type.selectedIndex){

case 0: form1.sub1.value=parseInt(form1.sta.value)*50;
break

case 1: form1.sub1.value=parseInt(form1.sta.value)*70;
break

case 2: form1.sub1.value=parseInt(form1.sta.value)*250;
break

}

switch(form1.type1.selectedIndex){

case 0: form1.sub2.value=parseInt(form1.clo.value)*50;
break

case 1: form1.sub2.value=parseInt(form1.clo.value)*70;
break

case 2: form1.sub2.value=parseInt(form1.clo.value)*250;
break

}

switch(form1.type2.selectedIndex){

case 0: form1.sub3.value=parseInt(form1.bac.value)*50;
break

case 1: form1.sub3.value=parseInt(form1.bac.value)*70;
break

case 2: form1.sub3.value=parseInt(form1.bac.value)*250;
break

}

form1.total.value=parseInt(form1.sub1.value)+parseInt(form1.sub2.value)+parseInt(form1.sub3.value);

if(parseInt(form1.total.value) > 1000){

form1.total.value=parseInt(form1.total.value)*0.9+5

}else if(parseInt(form1.total.value) > 500){

form1.total.value=parseInt(form1.total.value)*0.95+5

}else{

form1.total.value=parseInt(form1.total.value)+5

}

}
</script>
<body>
<form name="form1">
<select name="type">
<option>Standard</option>
<option>Clse-Up</option>
<option>Back Stage</option>
</select>
<input type="text" name="sta" size="3"/>
\$<input type="text" name="sub1" size="5" /><br />

<select name="type1">
<option>Standard</option>
<option>Clse-Up</option>
<option>Back Stage</option>
</select>
<input type="text" name="clo" size="3"/>
\$<input type="text" name="sub2" size="5" /><br />

<select name="type2">
<option>Standard</option>
<option>Clse-Up</option>
<option>Back Stage</option>
</select>
<input type="text" name="bac" size="3"/>
\$<input type="text" name="sub3" size="5" /><br />

<p><input type="button" value="Calculate" onClick="cal()"/>
<input type="reset" value="Reset" /></p>

<p>Total cost is: \$<input type="text" name="total" size="8"/></p>
</form>
</body>
</html>```

Hiroki Kozai

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted 08-11-2003 10:53

RTFM!!

Mathieu "POÏ" HENRI

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted 08-11-2003 15:34

RTFM would probably help, but here's another hint.
Get the value of each field you are checking using parseInt just like you are.
Show that value in an an alert.
It should show you where your error is.
Remember that null and zero are't the same thing.

Few problems in life can't be solved by chocolate

Kaniz
Bipolar (III) Inmate

From:
Insane since: Jun 2003

posted 08-12-2003 04:57

before the parsInt, could do a check to see if form.field.value = null, then set the value to zero.