-1

I have been working on this code for my GCSE assessment but I can't seem to get the JavaScript to work , I have looked through my code for a couple of weeks now and I still can't find the logic error within it I suspect that I have missed something out to get the Java to work , if anyone can help it would be extremely helpful

my current code I am looking through:

<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 \n";
 document.ExamEntry.name.focus();
 document.getElemenById('name').style.color="red";
 result = false;
 }

if (document.ExamEntry.subject.value=="") {
 msg+="You must enter the subject \n";
 document.ExamEntry.subject.focus();
 document.geElementById('subject').style.color="red";
 result = false;
 }

if (document.ExamEntry.exam number.value=="") {
 msg+="You must enter your exam number \n";
 document.ExamEntry.subject.focus();
 document.geElementById('exam number').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 id="exam number">Exam Number</td>
 <td><input type="text" name="Exam Number" /></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>

1 Answers1

1

Tidying up your snippet presents the real issue:

function ValidateForm() {
 var result = true;
 var msg="";

if (document.ExamEntry.name.value=="") {
 msg+="You must enter your name \n";
 document.ExamEntry.name.focus();
 document.getElemenById('name').style.color="red";
 result = false;
 }

if (document.ExamEntry.subject.value=="") {
 msg+="You must enter the subject \n";
 document.ExamEntry.subject.focus();
 document.geElementById('subject').style.color="red";
 result = false;
 }

if (document.ExamEntry.exam number.value=="") {
 msg+="You must enter your exam number \n";
 document.ExamEntry.subject.focus();
 document.geElementById('exam number').style.color="red";
 result = false;
 }

 if(msg==""){
 return result;
 }

 {
 alert(msg)
 return result;
}
<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 id="exam number">Exam Number</td>
 <td><input type="text" name="Exam Number" /></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>
SyntaxError: missing ) after condition  
if (document.ExamEntry.exam number.value=="") {  
---------------------------^

if your variable has a space in it, you cant use the dot notation, but you can use the square bracket notation. Change that line to:

if (document.ExamEntry["Exam Number"].value=="") {
Jamiec
  • 133,658
  • 13
  • 134
  • 193