I have a submit button like this:
<input class="create_button" name="commit"
onclick="return validate_activity();"
type="submit" value="Save">
I found that this button will always send request to the server whatever the validate_activity() return true or false?
What's the problem here?
UPDATE
Actually, I made a mistake in my validate_activity(), it makes me think it returned false, but it not.
I have a submit button like this:
<input class="create_button" name="commit"
onclick="return validate_activity();"
type="submit" value="Save">
I found that this button will always send request to the server whatever the validate_activity() return true or false?
What's the problem here?
UPDATE
Actually, I made a mistake in my validate_activity(), it makes me think it returned false, but it not.
Share Improve this question edited Jan 2, 2012 at 13:44 Rocky asked Dec 26, 2011 at 18:07 RockyRocky 5,7168 gold badges35 silver badges38 bronze badges 2 |6 Answers
Reset to default 10try doing same without return, e.g. onclick="validate_activity();"
and check if your function returns false in case of invalidity
Try this...
<form action="...." onsubmit="submitForm()" name="form">
<input type="submit" value="Save">
</form>
function submitForm(){
if(validationfails){
return false;
}
else {
document.form.submit();
return true;
}
}
<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform)" />
Inside the Javascript, when validated, do form.submit()
or else return false.
I hope this helps ! This will validate your form and submit it. This submit will fail if the Javascript is disabled in your browser.
if you want the submit to proceed even if the javascript is disabled in the client browser ( means no validation but will submit), then
<input type="button" name="commit"
value="Save"
onclick="validate_activity(myform);return false;" />
I hope no validation shouldn't hit you at all. But then javascript is just client side and I hope you will have serious sanity checks on the server side anyways and of many, I think people use it for very basic field checks.
You should use the submit
event of the form for validation..
<form action="..." method="..." onsubmit="return validate_activity()">
<...>
<input class="create_button" name="commit" type="submit" value="Save" />
<...>
</form>
If you're doing form validation then validating it on button's onclick
event, the way you're are trying makes no sense better you should use it on <form></form>
onsumbmit
event like this.
<form action="someURL" method="post" onsubmit="return validate_activity();">
</form>
I guess the problem is onclick="return validate_activity();"
, this return statement gets executed when clicking the submit button (not return false).
The correct way is onclick="validate_activity();"
and return false in validata_activity
function, which will stop the default submit action.
validate_activity()
? – Emmanuel N Commented Dec 26, 2011 at 18:13