Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1147
  • Last Modified:

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?
0
trickyidiot
Asked:
trickyidiot
  • 3
  • 2
  • 2
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now