return false is still submitting form - please help?

Discussion in 'JavaScript' started by joanna_l, Nov 16, 2007.

  1. #1
    Hi everyone,

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

    Thanks so much :)

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

    for (i=0; i < leng-2; i++)
    {
    var fieldname = myForm.elements.value;

    myForm.elements.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>
     
    joanna_l, Nov 16, 2007 IP
  2. ezpz

    ezpz Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First, you're returning false or true after checking the first value. So if the first value passes, the result will be true without checking the remaining values. Move return true to the end of the function.

    Second, if you're testing on Firefox you might need to replace myForm with document.myForm.

    Also, presumably the leng-2 is to skip the buttons. I'm not sure you can guarantee the elements will be returned in that order, you might need to explicitly exclude them (give them names to help).

    Keep going, you'll get there!
     
    ezpz, Nov 16, 2007 IP
  3. joanna_l

    joanna_l Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey ezpz...if only Javascript was easy peasy. I tried your suggestions and it still seems to be submitting. Here is what I changed:

    Did I say thanks? Thanks!

    function checkForm()
    {

    var leng = document.myForm.elements.length;
    var i=0;

    for (i=0; i < leng-2; i++)
    {
    var fieldname = document.myForm.elements.value;

    myForm.elements.focus();

    if(fieldname=="")
    {
    alert("The " + fieldname +" field is required.");
    fieldname.focus();
    return false;

    {
    else {
    alert("bleh");

    }

    }
    return true;
    }
     
    joanna_l, Nov 16, 2007 IP
  4. ezpz

    ezpz Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Putting alerts in is a good move. onSubmit needs to be in the FORM tag, not the submit button.
     
    ezpz, Nov 17, 2007 IP
  5. joanna_l

    joanna_l Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yeah, I know...I've been trying many options and changing that to onClick still doesn't work...doh.
     
    joanna_l, Nov 17, 2007 IP
  6. ezpz

    ezpz Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So your FORM tag now looks like this?

    <FORM NAME="myForm" METHOD="post" onsubmit="return checkForm()"
    ACTION="http://ss1.prosofttraining.com/cgi-bin/process.pl">
     
    ezpz, Nov 18, 2007 IP
  7. joanna_l

    joanna_l Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi, yes I have had that...but it didn't work. I also had the function called on the onClick on the submit button...that didn't work too...

    Don't worry about it. I just handed in as much as I could. I couldn't take it anymore...lol...

    Thanks for your help though!!!! :)
     
    joanna_l, Nov 18, 2007 IP
  8. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hello joanna,

    i played a little bit with your code and modified it, so that it works.

    You really need to check that your code is error free, otherwise some unforseen consequences are possible, like in your case :)

    One of the main errors was, that you set fieldname variable to an element value and checked it it its empty. When that was the case you called the focus() function on that value and that was wrong, because that function can only be called on an element and not its value.

    Like i said, always check your javascript if its error free first, use Firefox Error Console and make sure all warnings/errors are gone and then in most cases all shold work like it's supposed to ;)

    
    <html><head><title>test</title>
    
    <script language="javascript">
    function checkForm()
    {
    
    	var myForm = document.getElementById("myForm");
    	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=="")
    		{
    			alert("The " + fieldname +" field is required.");
    			myForm.elements[i].focus();
    			return false;
    		}
    			
    	}
    	
    	return true;
    }
    
    </script>
    </head>
    <body>
    
    <FORM id="myForm" 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>
    
    Code (markup):
     
    hogan_h, Nov 18, 2007 IP
  9. aRo`

    aRo` Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    should also try aptana as an open source java debugger framework
     
    aRo`, Nov 19, 2007 IP