views:

164

answers:

4
<form method="post" action="/Order/CheckOut/" onSubmit="return Validate()">

and then...

function Validate() {
    alert($("#email").val());
    return false;
}

The messed up part is when I take out the alert it works fine, the submit fails, but when I put the alert in, it allows the submit to go through... what the heck?

I also tried this:

function Validate() {
    if(document.getElementByID("email").value == "test"){
        alert("It says test.");
    }
    return false;
}

and got the same behavior, it would never make it to the return statement...

If I step through the JS with firebug and break at the alert or the if (depending on the version above) it stops there, and I click 'step into' and it just submits the form, why isn't it making it to the return false line?

+2  A: 

You could use event.preventDefault() instead.

$("form[name='myForm']").submit(function(e){
  e.preventDefault();
  alert($("#email").val());
});

You should attempt to keep your javascript, css, and html all seperate. Don't integrate them, or you'll make the project more difficult to manage. Instead of using the onsubmit attribute in HTML, simply append your logic to the $.submit() method of the form from your javascript as I did above.

This example assumes that you've given your form a name of "myForm." I merely used this in the example as you should itendify which form you're handling the submit-event of, and not use a generic $("form") selector.

Jonathan Sampson
+2  A: 

If you're already using jQuery you're doing the whole thing wrong to begin with. You shouldn't be manually specifying the onSubmit handler from within the <form> element. Do as @Jon suggested and just bind the submit event:

$("form").submit(function() {
    alert($("#email").val());
    return false;
});
cballou
This does not work, the form still submits. In fact if I put a breakpoint inside that function it never even breaks... I even tried specifying the form name like Jon said, to no avail.
shogun
@Ryan - You should use Firefox with Firebug enabled to see if you have any pre-existing javascript errors on page load. I would imagine you have something going on as the code is about as straightforward as it gets.
cballou
+1  A: 

Why not wrap it in a try block?

function Validate() {
    try {
        alert($("#email").val());
    } catch (e) {
        alert("Problem: " + e);
    }

    return false;
}
Pool
Ah ha! So this must mean that if an error happens it returns true? That's, odd.
shogun
Well it should help you identify the problem, but you still need to isolate out what went wrong.
Pool
Isn't the problem that no element with the ID `email` exists?
Pekka
no Pekka. And The Feast: I can see the error but what does this mean of how javascript reacts when it gets an error, why would it just fail out and return true to the onsubmit, or is it because it's failing it's just giving up on the onsubmit?
shogun
Ryan, there is nothing obvious wrong with the code which is why I suggested adding a try catch block to help you isolate the error. Take a look at http://jsbin.com/odixu , both methods seem to work.
Pool
I understand, at this point I am no longer worried about my code, I am just wondering about how Javascript tends to behave. It seems like because there was an error (of any kind) it causes the onsubmit function act as if it returned true, however I don't think that is the case, it may be that since it turned up an error, it just gave up on the onsubmit and continued with the submission of the form. I am just trying to understand how Javascript works in this regard, I suppose some engineer had to decide on this behavior, when error, continue with submit, rather than, on error, fail the submit.
shogun
@Ryan - onsubmit will return true unless you explicitely tell it otherwise. If you don't specify a return value or any return value other than false, it will submit as usual.
cballou
+1  A: 

The problem isn't that your function is returning true. The problem is that the JavaScript in the alert is failing and JavaScript quits interpreting the code. At which point, the browser is continuing with the default action which is to submit the form.

This is why Jonathan Sampson suggests using e.preventDefault() before the alert(), this way the browser doesn't continue with its default behaviour when the alert() fails.

Sandro