Jump to content

JavaScipt Newbie. Why is this code not working?


WillUK

Recommended Posts

<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 \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+=”You must enter the subject \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.asp">
<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>

Hi,

 

I am new to JavaScript and am trying to apply the above form validation code.

Would an experienced JavaScript developer please take a look and let me know why it is not processing? Am I missing some code? Calling it incorrectly?

I appreciate that this is pretty basic stuff but it is driving me nuts!

 

Info: success.asp does not yet exist.

 

Any help would be really appreciated.

 

Thanks

Will

Link to comment
Share on other sites

I would recommend reviewing the process of validating forms with JavaScript. Perhaps the following will help:

http://javascript.about.com/library/blvalsub1.htm

 

To start, change this

<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>

To

<td><input type="submit" name="Submit" value="Submit" onclick="validateForm(); return false;" /></td>

 

Edited by cyberRobot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.