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>
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!
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; }
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">
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!!!!
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):