# Thread: calculating a total from a form

1. ## calculating a total from a form

Hi all ...

Let me first of all warn you I'm not really good a Javascript (jet) [img]images/smilies/xeye.gif[/img]

I want to calculate a total for a form and alert if some conditions aren't met ...

I'm going completely crazy here ... so any help would be appreciated.. here's the deal ..

there is 4 input's with name A - D their values need to be between 0 -50 and the total must be 50 exactly .....

this is what I got so far
PHP Code:
``` function Optellen(){var totaltotal = 0A = document.CQS.A.valueB = document.CQS.B.valueC = document.CQS.C.valueD = document.CQS.D.valueif(A = '' ) { A = 0 }if(B = '' ) { B = 0 }if(C = '' ) { C = 0 }if(D = '' ) { D = 0 }total += eval(A)total += eval(B)total += eval(C)total += eval(D)if (total > 50){alert("The total can not be more than 50" )}else{document.CQS.total.value = total}}  ```
1 there must be an easier way .. two I keep getting NaN as an answer...

Edit:

I'm using the PHP code tag so I get nice outlined code but ofcourse it's javascript

2. Use parseInt() instead of 'eval()'. And then use isNaN() before validating the values.

3. Thanks I got it working now...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•