HTML Form - JavaScript onSubmit - make it stop!

<Form id="whatever" onSubmit="myFunction();returnFalse;">

How do I get the form to do the following on submit:
1) run my specified function
2) stop submitting - don't refresh the page - do not pass go - do not collect $100 - stop.

I have my reasons.

Ideas?
LVL 6
trickyidiotAsked:
Who is Participating?
 
amit_gCommented:
<form id="whatever" onsubmit="return myFunction();">

<script>
function myFunction()
{
    if (EverythingOK)
    {
        return true;
    }
    else
    {
        return false;
    }
}
</script>
0
 
trickyidiotAuthor Commented:
return false isn't stopping the form from submitting.
0
 
amit_gCommented:
It would stop it unless you have an error in your function. In your browser check for Javascript errors.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
amit_gCommented:
Another reason for it could be script submit i.e. if you did myForm.submit() then this onsubmit handler won't even be called. In that case you need to call and stop the submit process in the calling script itself.
0
 
BraveBrainCommented:
...or if you don't ever want to submit the form and don't want to return false through your function:
onsubmit="myFunction(); return false;"

If your function checks certain criterias that if met should let the form be submitted you should use amit_g's suggestion, where the
return true
tells the form to submit

Also note that a script error in myFunction() would make the form keep submitting in IE if you use
onsubmit="myFunction(); return false;"
and also in some cases in the other example
0
 
trickyidiotAuthor Commented:
Amit - You get the points on behalf of my stupidity.

I already had return false;... kind of

I had returnFalse; for some bonehead reason.

For my stupidity, you gain.
0
 
BraveBrainCommented:
Also note that I started typing my previous comment when the first comment from amit_g was the only one there, in case you feel I'm repeating what's already mentioned ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.