2 Replies - 6040 Views - Last Post: 17 November 2007 - 11:08 AM

#1 joanna_l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-November 07

return false still submitting my form

Posted 17 November 2007 - 12:34 AM

Hi,

I have this problem with my return false's not working - my form still submits! Any ideas?

Thanks so much :)

function checkForm()
 {
	var leng = myForm.elements.length;
	var i=0;

	for (i=0; i < leng-2; i++) 
	{
		var fieldname = myForm.elements[i].value;
		
		myForm.elements[i].focus();
		
		if(fieldname.value=="") 
		{
			alert("The " + fieldname +" field is required.");
			fieldname.focus();
			return false;
			
		} 
		else 
		{ 
		
		return true; 
		}

	}
	
}



<FORM NAME="myForm" METHOD="post"
ACTION="http://ss1.prosofttraining.com/cgi-bin/process.pl">
Name:<BR>
<INPUT TYPE="text" size="30" NAME="name"><br>
Email address:<BR>
<INPUT TYPE="text" size="30" NAME="email address" onblur="emailTest(this);"><br>
 Phone number:<BR>
<INPUT TYPE="text" size="30" NAME="phone number"><br>
Fax number:<BR>
<INPUT TYPE="text" size="30" NAME="fax number" ><p>
<INPUT TYPE="submit" VALUE="Submit Data" onsubmit="return checkForm()">
<INPUT TYPE="reset" VALUE="Reset Form">
</FORM>


edit: Added code tags... oh and this is javascript, not java. They are two different things.

This post has been edited by Martyr2: 17 November 2007 - 12:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: return false still submitting my form

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: return false still submitting my form

Posted 17 November 2007 - 12:54 AM

Is your alert() actually firing?

This post has been edited by girasquid: 17 November 2007 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,179
  • Joined: 18-April 07

Re: return false still submitting my form

Posted 17 November 2007 - 11:08 AM

You had a few things wrong with this, but all the main pieces were there. So for that good work. I have made the corrections and a working copy is below.

Some notes....

1) I assume that your javascript is inside javascript tags. Be it in the <head> tags preferably.

2) You call the function using onsubmit IN THE FORM TAG, not the submit button itself.

3) Your if statement returning true and false had to be redone. You only call the return true after the for loop has successfully gone through all elements without error. If there was an error, you would return false. The way you had it, if the first element was ok, it would return true for the entire form.

4) I am not sure if your onblur event was defined, but it had to be taken out in my example. Make sure it is back in if, and only if, you have the function it calls defined (emailTest)

5) You were referencing the field attributes when you had only stored the field's value. fieldname was the fields value, not its name and it contained a standard value, not a reference to the element itself.

So take a look and see how I did the example. I have put comments in the code to make it more readable and step by step fashion.

<html>
<head>
<title>Check Form</title>
<script type="text/javascript">

// Validates form on submit
function checkForm()
{
	// Get the length of the elements
	var leng = myForm.elements.length;
	var i=0;
	
	// Loop through the elements
	for (i=0; i < leng-2; i++)
	{
		// Get the fields value, not a reference to the field itself.
		var fieldname = myForm.elements[i].value;
		
		// Set the focus to the field
		myForm.elements[i].focus();
		
		// fieldname has a value, not the reference so just use the name.
		if (fieldname == "")
		{
			// Since you have just a value, you can't set focus. 
			// Again it is not a reference to the field, just a value.
			alert("The " + myForm.elements[i].name +" field is required.");
			return false; 
		}
	}

	return true;
}
</script>
</head>

<body>
<FORM NAME="myForm" METHOD="post"
ACTION="http://ss1.prosofttraining.com/cgi-bin/process.pl" onsubmit="return checkForm()">
Name:<BR>
<INPUT TYPE="text" size="30" NAME="name"><br>
Email address:<BR>
<INPUT TYPE="text" size="30" NAME="email address"><br>
Phone number:<BR>
<INPUT TYPE="text" size="30" NAME="phone number"><br>
Fax number:<BR>
<INPUT TYPE="text" size="30" NAME="fax number" ><p>
<INPUT TYPE="submit" VALUE="Submit Data">
<INPUT TYPE="reset" VALUE="Reset Form">
</FORM>
</body>
</html>



I hope you find the example useful and solves your problems. Enjoy!

"At DIC we be javascripting form validating code ninjas!" :ph34r:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1