By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

How to stop form submit when onClick event returns false

P: n/a

Folks

I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">

I have tested performPrePostChecks() - Its a long script, but I have proven
its success by having an alert box echo the success with a simple true or
false message like so:

<script type="text/javascript">
function performPrePostChecks()
{
if(checkRequiredFormFields('form1','headline','Hea dline')==false)
{ alert("false");
return false;
}

alert("true");
return true;
}
</script>
How come that irrespective of the function performPrePostChecks returning
true, or false, that the form is still submitted?

Can someone point me in the right direction?

Thanks,
Randell D.
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Randell D." <re**********************@and.share.com> wrote in
news:g%Wwb.497470$pl3.300911@pd7tw3no:
I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">
[snip]
How come that irrespective of the function performPrePostChecks
returning true, or false, that the form is still submitted?


Because that's what you asked it to do. To be more specific, an action
handler is treated as if it's a function, with the return value determining
whether or not to go ahead. You don't return anything, so the default is
"yes, go ahead." Try returning the result of your checking function.

Is it possible that you're used to Perl, where a function (aka sub) by
default returns the value of the last statement executed? Javascript
doesn't use that convention; if you want to return a value, you have to do
so explicitly.
Jul 20 '05 #2

P: n/a
RR
> onClick="this.value='Saving...'; performPrePostChecks();">

To clarify Eric's correct comments, you want:
onClick="this.value='Saving...'; return performPrePostChecks();"

regards,
RR
Jul 20 '05 #3

P: n/a
put the funtion that returns fale in inte forms onsubmit handler intead of
the submit button.

Stu

"Randell D." <re**********************@and.share.com> wrote in message
news:g%Wwb.497470$pl3.300911@pd7tw3no...

Folks

I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">

I have tested performPrePostChecks() - Its a long script, but I have proven its success by having an alert box echo the success with a simple true or
false message like so:

<script type="text/javascript">
function performPrePostChecks()
{
if(checkRequiredFormFields('form1','headline','Hea dline')==false)
{ alert("false");
return false;
}

alert("true");
return true;
}
</script>
How come that irrespective of the function performPrePostChecks returning
true, or false, that the form is still submitted?

Can someone point me in the right direction?

Thanks,
Randell D.

Jul 20 '05 #4

P: n/a

"RR" <ne****************@spamgourmet.com> wrote in message
news:Ke*****************@news-server.bigpond.net.au...
onClick="this.value='Saving...'; performPrePostChecks();">


To clarify Eric's correct comments, you want:
onClick="this.value='Saving...'; return performPrePostChecks();"

regards,
RR


Luvely Juvely... thanks to both of you - and the nice thing is, I actually
understand where I went wrong and why it should be the way you guys
suggested...

Thanks for the help,
randell d.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.