What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

Problem with my validation script!

curtm8

Junior Member
Hello gentlemen,

I have a problem with my GCSE coursework and I cant quite pinpoint the problem and was wondering if you guys could help me!:confused::rolleyes::cool::eek:
Here is my code!
Code:
<head>

<title>Exam entry</title>

<script language="javascript" type="text/javascript">

function validateForm() {
	
var result = true;
var msg="";
var entry = document.querySelector('input[name="grade"]:checked').value;
var answer = confirm("You have chosen" + " " + entry + ", are you sure?");
var DG = Working;
	DG+= '1';
	if (document.ExamEntry.name.value=="") {
		msg+="You must enter your name \n";
		document.ExamEntry.name.focus();
		document.getElementById('name').style.color="red";
		result = false;
		DG+= '2';
	}
	
	if (document.ExamEntry.subject.value=="") {
		msg+="You must enter the subject \n";
		document.ExamEntry.subject.focus();
		document.getElementById('subject').style.color="red";
		result = false;
		DG+= '3';
	}
	
	if(answer){
		alert("You have confirmed that you chosen" + " " + entry + "!");
		DG+= '4';
	}
	else{
		alert("Please change to appropriate entry!");
		result = false;
		DG+= '5';
	}
	if(document.ExamEntry.examno.value.length !== 4) { 
		msg+="You must enter a four digit examination number \n";
		document.ExamEntry.examno.focus();
		document.getElementById('examno').style.color="red";
		result = false;
		DG+= '6';
	}
		
	if(msg==""){
		return result;
			}
	{
	alert(msg);
	DG+= '7';
	return result;
	alert(DG);
	}
	}

</script>
</head>

<body>
<h6 id="Debug">Working...</h6>
<h1>Exam Entry Form</h1>
<blockquote>
  <blockquote>
    <form name="ExamEntry" method="post" action="success.html">
    <table width="52%" 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="examno">Examination Number</td>
          <td><input name="examno" type="text" maxlength="4" /></td>
          
        </tr>
        
        <tr>
		<td id="grade">Level of Entry</td>
		<td>
        <input type="radio" name="grade" id="GCSE" value="GCSE" checked="checked"/>GCSE
		<input type="radio" name="grade" id="AS" value="AS"/>AS
        <input type="radio" name="grade" id="A2" value="A2"/>A2</td>
        </tr>
        
        <tr>
          
          <td></td>
          <td><input align="right" type="submit" name="Submit" value="Submit" onClick="return validateForm();" /><input align="right" type="reset" name="Reset" value="Reset" /></td>
          
        </tr>
        
      </table>
    </form>
  </blockquote>
</blockquote>
</body>
 
I would suggest using this instead. Use an input button instead of a submit button.

<input align="right" type="button" name="Submit" value="Submit" onClick="submitForm()" />

<script>
function submitForm(){
if(validateForm()){
document.getElementById("ExamEntry").submit();
}
}
</script
 
You can also try out by adding - "onSubmit=validateForm();" on the <form....> line after the action property. I think that would help too without changing much on your code.
 
Back
Top