<html> <head> <title>Exam entry</title> <script language="javascript" type="text/javascript"> function validateForm() { var result = true; var msg""; if (document.ExamEntry.name.value=="") { msg+="You must enter your name here \n"; document.ExamEntry.name.focus(); document.getElementById('name').style.color="red"; result = false; } if (document.ExamEntry.subject.value=="") { msg+="You must enter the subject here \n"; document.ExamEntry.subject.focus(); document.getElementById('subject').style.color="red"; result = false; } if(msg==""){ return result; } { alert(msg) return result; } } </script> </head> <body> <h1>Exam Entry Form</h1> <form name="ExamEntry" method="post" action="success.html"> <table width="50%" border="0"> <tr> <td id="name">Name</td> <td><input type="text" name="name" /></td> </tr> <tr> <td id="subject">Subject</td> <td><input type="text" name="subject" /></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td> <td><input type="reset" name="Reset" value="Reset" /></td> </tr> </table> </form> </body> </html> Question: what's wrong with this code? It's supposed to bring an error message if you don't fill the fields. It doesn't, it always take me straight to the success page. This is the sample code we've been given as a base, and it doesn't work... it really bugs me, either there's meant to be an error or I've mistyped, but I've quad checked and I don't think they'd purposely put an error in? Ty for any help
Guess what? I just learned javascript! Weee! No clue if it'll pass the info through properly, as I've only really had a few minutes of self-induced learning, but it validates properly and seems to work as best I can tell. Code: <html> <head> <title>Exam entry</title> <script type="text/javascript"> function validateForm() { if (document.ExamEntry.name.value=="") { document.ExamEntry.name.focus(); document.getElementById('name').style.color="red"; alert("You must enter name here!"); return false; } else if (document.ExamEntry.subject.value=="") { document.ExamEntry.subject.focus(); document.getElementById('subject').style.color="red"; alert("You must enter subject here!"); return false; } else { return true; } } </script> </head> <body> <h1>Exam Entry Form</h1> <form name="ExamEntry" method="post" onsubmit="return validateForm()" action="success.html"> <table width="50%" border="0"> <tr> <td id="name">Name</td> <td><input type="text" name="name" /></td> </tr> <tr> <td id="subject">Subject</td> <td><input type="text" name="subject" /></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td><input type="reset" name="Reset" value="Reset" /></td> </tr> </table> </form> </body> </html> Enjoy! Edit: guess I should point out the errors. The "onsubmit" was incorrectly placed, the if statement wasn't correct, the popup message wasn't an alert (as it should have been), and the order of operations was incorrect. There was also a lot of extra stuff that I couldn't figure out at all! So I just deleted it and put in something else.
Thank you so much And yes, the errors at what I'm most interested in, being a novice in JS/HTML, and trying to learn it. so thank you very very much
If you have Firefox installed you can get an add on called the web developer toolbar that can help with web based stuff
Also, Firebug is very useful for Firefox based web-dev. I mainly use it for CSS debugging but it will also very ably do javascript.
+1 for Firebug. For more advanced web programming *cough*hacking*cough* then Firecookie becomes useful. And before you start bashing me that hacking is illegal, immoral etc. I mean the find-security-vunerabilities type of hacking, not destroy-everything hacking. And anyway, the second one should be called cracking, because it damages a system/data. /offtopic
No problem, Arghnews! I was really bored last night and figured it would be worth a lark to see if I could figure something out, fully expecting not to do much of anything worthwhile. For the record I basically just had notepad open on one corner of my screen with code in, firefox taking up the right-hand pane, and the built in error console (CTRL+SHIFT+J) in the empty, remaining space to diagnose it as I slowly learned how javascript worked. I've done a very minor bit of HTML many years ago, but never really dabbled in JS before, so this was a very fun experience for me!