Javascript problems

  • Thread starter emad
  • 1 comments
  • 398 views
5,622
eMadman
I've got an assignment for my javascript class and it just doesn't want to work right. Currently, I'm trying to code Validate function of the javascript. The validate function's purpose is to make sure that the name is entered and to make sure that the email address entered is valid. Meaning that there has to be at least 1 character after the @, 2 after the dot, etc. The code I'm using right now has been copied out of an example from the book but with a few changes to make it check the name as well.

The problem right now is that the code picks up that the email address is invalid, but for some reason it submits the form anyways. Any suggestions on what I can do for this?

Code:
<html>
<head>
<title>Emad Ghazipura</title>

<script type="text/javascript" language="JavaScript">
<!--

function validate(){
	var email = document.quiz.email.value;
	var at = email.indexOf("@",1);
	var dot = email.indexOf(".",at+2);
	var len = email.length;
		
	if (!document.quiz.name.value)
		{
		alert("You must enter your name.");
		document.quiz.name.focus();
		return false;
		}
	else if (!(at > 0 && dot > 0 && len > dot+2))
		{
		alert("Your Email is invalid");
		email.focus();
		return false;
		}
	else
		{
		return true;
		}
}
//-->
</script>

<script type="text/javascript" language="JavaScript">
<!--

function answer(){
	

}
//-->
</script>

</head>




<center><h1>ITM 405 Project 2</h1>

<p>Section 905</p>

<form name=quiz method=post action="mailto:eghazipu@ryerson.ca" onsubmit="return validate();">
	<table width=75% border=0 align=center>
	<tr>
		<td align=right>Your Name (Required)</td>
		<td><input name="name" type=text size=40 maxlength=25></td>
	<tr>
		<td align=right>E-mail Address (Required):</td>
		<td><input name="email" type=text size=20 maxlength=25></td>

	<tr>
		<td align=right width=50%>question 1</td>
		<td width=50%><select name=q1>
		<option value=1>1</option>
		<option value=2>2</option>
		<option value=3>3</option>
		<option value=4>4</option>
		</select>
		</td>
	</tr>
	<tr>
		<td align=right width=50%>question 2</td>
		<td width=50%><select name=q2>
		<option value=1>1</option>
		<option value=2>2</option>
		<option value=3>3</option>
		<option value=4>4</option>
		</select>
		</td>
	</tr>
	<tr>
		<td align=right width=50%>question 3</td>
		<td width=50%><select name=q3>
		<option value=1>1</option>
		<option value=2>2</option>
		<option value=3>3</option>
		<option value=4>4</option>
		</select>
		</td>
	</tr>
	<tr>
		<td align=right width=50%>question 4</td>
		<td width=50%><select name=q4>
		<option value=1>1</option>
		<option value=2>2</option>
		<option value=3>3</option>
		<option value=4>4</option>
		</select>
		</td>
	</tr>
	<tr>
		<td align=right><input type=submit value="Check Answers" onClick="answer();")></td>
		<td>Your score is: <input name=score type=text size=10></td>
	<tr>
		<td align=right><input type=submit value="Submit the Form"></td>
		<td><input type=reset value="Reset the Form"></td>
	</tr>
	</table>
</form>


</html>
 
I think your problem is to do with the HTML code itself...

You are using the "onsubmit" event on the form to call validate...

I would suggest using an "onclick" event on the submit button and then call form.submit() from the validate function...

Your problem basically is that you have submitted before you've validated!?

C.
 
Back