By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

error: object expected

P: 9
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 :)

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>activity 2.22</title>
  4. </head>
  5. <script type="text/javascript">
  6. function validateform()
  7. {
  8.   var element;
  9.   var BikeMoney;
  10.   var TVMoney;
  11.   var iPodMoney;
  12.   var CarPrice;
  13.   var flag="OK";
  14.   element=document.getElementsByTagName('input');
  15.   for (counter=0; counter<element.length; counter++)  
  16.     {
  17.       switch (element[counter].type)
  18.         {
  19.           case "submit":
  20.           break;
  21.           default:
  22.           if(isNaN(element[counter].value))
  23.             {
  24.               alert("You need to enter a number into " + element[counter].name);
  25.               var flag="NotOK";
  26.             }
  27.           else
  28.           {
  29.             BikeMoney=element[0].value;
  30.             TVMoney=element[1].value;
  31.             iPodMoney=element[2].value;
  32.             CarPrice=element[3].value;
  33.           }
  34.         }
  35.     }
  36. }
  37. if (var flag=="OK")
  38.   {
  39.     TotalMoney = parseFloat(BikeMoney) + parseFloat(TVMoney) + parseFloat(iPodMoney);
  40.       if (TotalMoney >= CarPrice)
  41.         {
  42.           alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you can afford the car");
  43.         }
  44.     else
  45.         {
  46.           alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you cannot afford the car");
  47.         }
  48.   }
  49. </script>
  50. <body>
  51. <form name="inputform" method="post" action="">
  52. <table>
  53.   <tr><td>Enter money from bike sale</td><td><input type="text" name="Bike Money"></td></tr>
  54.   <tr><td>Enter money from TV sale</td><td><input type="text" name="TV Money"></td><tr>
  55.   <tr><td>Enter money from iPod sale</td><td><input type="text" name="iPod Money"></td><tr>
  56.   <tr><td>Enter the price of the car</td><td><input type="text" name="Car Price"></td></tr>
  57.   <tr><td></td><td><input type="submit" value="Submit Details" onclick=validateform();
  58. </table>
  59. </form>
  60. </body>
  61. </html>
Nov 10 '09 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 5K+
P: 8,639
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)
Nov 10 '09 #2

P: 9
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 :)
Nov 10 '09 #3

Expert Mod 5K+
P: 8,639
Expand|Select|Wrap|Line Numbers
  1. if (var flag == "OK") // your code
Expand|Select|Wrap|Line Numbers
  1. if (flag == "OK") // instruction code
Nov 10 '09 #4

P: 9
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!!
Nov 10 '09 #5

Expert Mod 5K+
P: 8,639
glad you finally got it working :)
Nov 10 '09 #6

P: 2
Just wondering what the final outcome was, as i myself have the same javascript example...

Am having trouble getting the calculation to execute.

Jul 26 '10 #7

P: 2
Anyone help?
Aug 2 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.