Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onsubmit = 'return false' not working in IE

Status
Not open for further replies.

spacedeveloper

Programmer
Aug 3, 2010
27
US
Hi everyone,

I'm wondering:

why does my form STILL submit in IE after this code is written (it does NOT submit in other browsers)?:


<form action=" onsubmit="return false;" method="post">


My button is a type="submit" button.

Thanks for any help
 
It works for me.

I would ask what else you are doing there, as having a form that can never be submitted seems a bit pointless.

If you have more JS being executed there may be an error there preventing the "return false;" statement from actually working.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Well, the original form code was a call to a js function, like this:

<form action=" onsubmit="return validateForm();" method="post">

And here is the (condensed) js:

<script language="javascript" type="text/javascript">
function validateForm() {
var infoBox = document.getElementById("field_cust_209006");
var infoBoxError = document.getElementById("errorMsg_cust_209006");
var noerror = true;

if (infoBox.value.length > 254) {
noerror = false;

infoBoxError.innerHTML = "Additional Info. must be 255 characters or less. (currently " + infoBox.value.length + " characters long)";
infoBoxError.style.display = "block";
infoBox.style.border = "solid 1px #CC3333";
}
else {
infoBox.style.border = "1px solid grey";
infoBoxError.style.display = "none";
}

return noerror;
}
</script>


I've tested the js all the way through with alerts and it successfully returns "false" when necessary, but no matter what the form will ALWAYS submit (in IE). I can't figure out why.
 
The only time your form would not be submitted is when the length of your input is greater than 254 characters.

Your code works fine in that respect for me.

For testing purposes reduce that value to say 4 or 5 characters and try it again.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
But that's the issue: it gets submitted EVEN if there are more than 254 characters and I have no idea why.
 
Here's what I did with your code and it works just fine.
Code:
<html>
<head></head>
<script language="javascript" type="text/javascript">
        function validateForm() {
            var infoBox = document.getElementById("field_cust_209006");
            var infoBoxError = document.getElementById("errorMsg_cust_209006");
            var noerror = true;

            if (infoBox.value.length > [red]4[/red]) {
                noerror = false;

                infoBoxError.innerHTML = "Additional Info. must be 255 characters or less. (currently " + infoBox.value.length + " characters long)";
                infoBoxError.style.display = "block";
                infoBox.style.border = "solid 1px #CC3333";
            }
            else {
                infoBox.style.border = "1px solid grey";
                infoBoxError.style.display = "none";                
            }

            return noerror;
        }
    </script>
<body>
<form action="[URL unfurl="true"]www.website.com"[/URL] method="POST" onsubmit="return validateForm();">
<input type="text" name="tbox" id="field_cust_209006" value="some text here">
<div id="errorMsg_cust_209006"></div>
<input type="submit" name="sub" value="submit this form">
</form>


</body>
</html>
I just changed the value down to 4 to make it easier to test. I won't submit when its more than 4. It will simply make he modifications to the text box and add the text to the div to show he error.

If yours is submitting then there's something else going on you are not showing us. Are you maybe getting JS errors you have not checked?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Make sure that those ID values you are using exist somewhere on the page. If they don't exist, then that can cause the function to exit and allow the form to submit.
 
Even if I change the number of characters, the page still submits if I exceed entering the # of characters into the field. So, I guess it's definitely something else.

I do not get any js errors and I've been looking in IE's Developer Tools Script tab for debugging. Cannot figure out what's going on here.

This is frustrating.
 
Borvik, yeah I double checked: those ID values do exist on the page elsewhere.
 
Is there anyway we can see the actual page live?
If you try the code above in an empty page does it still behave the same way?

Is there other JS code that may be interfering?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Actually, the page is on an intranet site at my company.

Vacunita, the code above that you posted works fine for me as well! Wierd. So, it's gotta be something else in my code and not the browser per se.

I'll post all of my js and see if there's anything I'm missing:

Code:
<HEAD>
    <script language="javascript" type="text/javascript">
        function validateForm() {
            var referralEMail = document.getElementById("field_cust_212005");
            var referralEMailError = document.getElementById("errorMsg_cust_212005");
            var infoBox = document.getElementById("field_cust_209006");
            var infoBoxError = document.getElementById("errorMsg_cust_209006");
            var noerror = true;

            if (referralEMail.value.length > 0) {
                var apos = referralEMail.value.indexOf("@");
                var dotpos = referralEMail.value.lastIndexOf(".");

                if (apos < 1 || dotpos - apos < 2) {
                    noerror = false;

                    referralEMailError.innerHTML = "The Referral Email is not in a correct format";
                    referralEMailError.style.display = "block";
                    referralEMail.style.border = "solid 1px #CC3333";
                }
            }
            else {
                referralEMail.style.border = "1px solid grey";
                referralEMailError.style.display = "none";                
            }
            if (infoBox.value.length > 254) {
                noerror = false;

                infoBoxError.innerHTML = "Additional Info. must be 255 characters or less. (currently " + infoBox.value.length + " characters long)";
                infoBoxError.style.display = "block";
                infoBox.style.border = "solid 1px #CC3333";
            }
            else {
                infoBox.style.border = "1px solid grey";
                infoBoxError.style.display = "none";                
            }

            return (noerror);
        }
    </script>
</HEAD>

Thanks again for any help (and your efforts so far!)

 
Ok, so we can't see a live page - how about the full code for the page - not just the javascript? That way we can analyze the DOM as well.
 
The code still works fine or me. Both the email check,and the character length check work as expected, and the form is not submitted unless both are correct.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
spacedeveloper said:
Vacunita, the code above that you posted works fine for me as well! Wierd. So, it's gotta be something else in my code and not the browser per se.

Are you calling the form's submit method directly at any stage?

If so, it will completely bypass any onsubmit handler and submit the form regardless.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
BillyRay, no I'm not directly calling the form's submit method from anywhere else in the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top