views:

72

answers:

5

I have a form that has a submit button in it somewhere.

However, I would like to somehow 'catch' the submit event and prevent it from occurring.

Is there some way I can do this?

I can't modify the submit button, because it's part of a custom control.

+1  A: 

On your form element, add an onSubmit attribute and put some javascript in it - return false to stop the submit.

<form method="POST" action="somepage" onSubmit="if(!Valid()){return false;};">

Where Valid is a javascript validation function.

If Valid returns true for valid or false for invalid, this can be shortened to:

onSubmit="return Valid();"
Oded
Doesn't seem to be working: http://jsfiddle.net/BJEmR/
George Edison
@George Edison - You are calling a javascript function. Call it properly and return it from the `onSubmit` handler; `return test();`. You should also have the correct form elements.
Oded
@Oded: I fixed it like you suggested, but it still won't work: http://jsfiddle.net/L4Abh/
George Edison
@George Edison - "submit" is a very bad name for a javascript function.
Oded
+6  A: 

you can use onSubmit like this

<form onsubmit="alert('stop submit'); return false;" >

or

<javascript>
   function toSubmit(){
      alert('I will not submit');
      return false;
   }
</javascript>

<form onsubmit="return toSubmit();" >

demo

Reigel
As mentioned in the above answer, this doesn't seem to be working: http://jsfiddle.net/BJEmR/
George Edison
you got errors on your demo... check my demo...
Reigel
I put in your demo: http://jsfiddle.net/L4Abh/ Still doesn't work.
George Edison
for some reason, function named `submit()` can not be used, try again here http://jsfiddle.net/L4Abh/3/ ... I've change submit to toSubmit
Reigel
@Reigel: Ah, thanks!
George Edison
@Reigel: I'm guessing this is because of the naming conflict with the form's submit() method (i.e., the method you'd use to submit the form programmatically)
Tim Goodman
@Tim @Reigel: Exactly. Elements become expando properties of their parent form, by name (`<input name="foo">` will become `formObject.foo`). Same for elements named `submit`, they hide the built-in method of that name. Since JS is case sensitive, calling it `Submit` would already work.
Tomalak
+1  A: 
var form = document.getElementById("idOfForm");
form.onsubmit = function() {
  return false;
}
naikus
A: 

You can do it with jquery.

http://jquery-howto.blogspot.com/2009/05/disable-submit-button-on-form-submit.html

joel3000
That disables it. In my question I mentioned 'catching it.'
George Edison
That shows you how to catch the submit without touching the form at all, all it needs the #id. You can rewrite the function as you please.
joel3000
+2  A: 

Unlike the other answers, return false is only PART of the answer. Consider the following

html

<form onsubmit="return mySubmitFunction()">
</form>

script

function mySubmitFunction()
{
  someBug()
  return false;
}

return false won't be executed and your form will be submitted (bit of a pain to debug). You also need to use preventDefault()

function mySubmitFunction(event)
{
  event.preventDefault();
  someBug();
  return false;
}

In this case, even with the bug the form won't submit!

Ben Rowe
Thanks for pointing that out!
George Edison