error: object expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ibys
    New Member
    • Nov 2009
    • 8

    error: object expected

    hi,
    hoping i have posted this in the right section this time :)
    i am doing my javascript still, and cant figure out why i keep getting an error: object expected message for validateform()
    im just not sure what i have done wrong with it, i have tried to give it a function, just cant find where my mistake is. any help would be greatly appreciated, thanks :)

    Code:
    <html>
    <head>
    <title>activity 2.22</title>
    </head>
    <script type="text/javascript">
    function validateform()
    {
      var element;
      var BikeMoney;
      var TVMoney;
      var iPodMoney;
      var CarPrice;
      var flag="OK";
      element=document.getElementsByTagName('input');
      for (counter=0; counter<element.length; counter++)  
        {
          switch (element[counter].type)
            {
              case "submit":
              break;
              default:
              if(isNaN(element[counter].value))
                {
                  alert("You need to enter a number into " + element[counter].name);
                  var flag="NotOK";
                }
              else
              {
                BikeMoney=element[0].value;
                TVMoney=element[1].value;
                iPodMoney=element[2].value;
                CarPrice=element[3].value;
              }
            }
        }
    }
    if (var flag=="OK")
      {
        TotalMoney = parseFloat(BikeMoney) + parseFloat(TVMoney) + parseFloat(iPodMoney);
          if (TotalMoney >= CarPrice)
            {
              alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you can afford the car");
            }
        else
            {
              alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you cannot afford the car");
            }
      }
    </script>
    <body>
    <form name="inputform" method="post" action="">
    <table>
      <tr><td>Enter money from bike sale</td><td><input type="text" name="Bike Money"></td></tr>
      <tr><td>Enter money from TV sale</td><td><input type="text" name="TV Money"></td><tr>
      <tr><td>Enter money from iPod sale</td><td><input type="text" name="iPod Money"></td><tr>
      <tr><td>Enter the price of the car</td><td><input type="text" name="Car Price"></td></tr>
      <tr><td></td><td><input type="submit" value="Submit Details" onclick=validateform();
    </table>
    </form>
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    your problems are line #36 and #37.

    on line #36 you end the function and the remaining code is not executed onclick, but immediately.

    on line #37 you simply have a syntax error (even if this were valid, it would always evaluate to false)

    PS. I didn’t get an object expected error…

    PPS. please don’t use tables to layout your form, that’s outdated. a better approach

    PPPS. there’s room for improvement, because you assign the input values four times (i.e. in every loop cycle) (where once is enough)

    Comment

    • Ibys
      New Member
      • Nov 2009
      • 8

      #3
      ok, I think I have fixed the first error you mentioned, but how would you recommend i fix the syntax error? not too sure what I have done wrong with that line, as it was in the instructions for how to complete the code, to insert an 'if' statement with "if flag=="OK"", then to put in the coding that is below to make the correct alert come up, which worked in the more simple code i copied it from.

      I have found I only get an object expected error when using internet explorer, I only get a syntax error with firefox. Im only using the table format as this is what im being assessed on in this module, but thanks for the link, I know to use that later on now :)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by Ibys
        not too sure what I have done wrong with that line, as it was in the instructions for how to complete the code, to insert an 'if' statement with "if flag=="OK""
        compare
        Code:
        if (var flag == "OK") // your code
        with
        Code:
        if (flag == "OK") // instruction code

        Comment

        • Ibys
          New Member
          • Nov 2009
          • 8

          #5
          ok i have managed to fix it now, i went back through until i could figure out what i had done wrong, i had been trying to declare it as a variable again, while sking it in an if statement- still only a beginner, so i still am making silly little mistakes :) thanks for the help!!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            glad you finally got it working :)

            Comment

            • Clay2010
              New Member
              • Jul 2010
              • 2

              #7
              Just wondering what the final outcome was, as i myself have the same javascript example...

              Am having trouble getting the calculation to execute.

              Cheers

              Comment

              • Clay2010
                New Member
                • Jul 2010
                • 2

                #8
                Anyone help?

                Comment

                Working...