SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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 
    total
    total 
    0
    document.CQS.A.value
    document.CQS.B.value
    document.CQS.C.value
    document.CQS.D.value

    if('' ) { }
    if(
    '' ) { }
    if(
    '' ) { }
    if(
    '' ) { }

    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...

    Please help

    Edit:

    I'm using the PHP code tag so I get nice outlined code but ofcourse it's javascript
    the neigbours (free) WIFI makes it just a little more fun

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use parseInt() instead of 'eval()'. And then use isNaN() before validating the values.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I got it working now...
    the neigbours (free) WIFI makes it just a little more fun


Bookmarks

Posting Permissions

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