views:

166

answers:

6
<input type="image" src="images/join.gif" name="SubmitStudent" onClick="CheckCaptcha();CheckTermsAcceptance(document.getElementById('chkStudent'));" width="98" height="31"/>

In the above code, if either the function CheckCaptcha or CheckTermsAcceptance returns False, than Form should not be submitted. How to check this?

+1  A: 

You may use the code below:

function validate(){
return (CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent')));

}

<input type="image" src="images/join.gif" name="SubmitStudent" onclick="return validate()" width="98" height="31"/>
Billy
+2  A: 

To abort from an onclick handler, return false.

return CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent'));
Chuck
A: 

I would make it a separate function

<input type="image" src="images/join.gif" name="SubmitStudent" onClick="mySubmit();" width="98" height="31"/>
<script type="text/javascript">
function mySubmit() {
  if(!CheckCaptcha()) return false;
  if(!CheckTermsAcceptance(document.getElementById('chkStudent'))) return false;
  else document.myform.submit();
}
</script>
Jefim
`if(boolean) return false;` is not great code, you should just return the boolean. You don't need to submit the form explicitly.
DisgruntledGoat
+2  A: 
<input onClick="return onclickEvent(e)" width="98" height="31" type="image" src="images/join.gif" name="SubmitStudent" />

function onclickEvent(e){
  return CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent'));
}

Or even

<input onClick="return CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent'))" width="98" height="31" type="image" src="images/join.gif" name="SubmitStudent" />

Much better with jquery:

<input id="your-id" type="image" src="images/join.gif" name="SubmitStudent" width="98" height="31"/>


$("your-id").click(function(){
    return CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent'));
});
Victor
A: 

Here it is:

<input type="image" src="images/join.gif" name="SubmitStudent" onClick="return (CheckCaptcha() && CheckTermsAcceptance(document.getElementById('chkStudent')))" width="98" height="31"/>
Ramiz Uddin
+1  A: 

What about not using onclick altogether? I'm surprised how much I see this stuff here. Bind a listener to the button, start by preventing the default behaviour either returning false or using the browser-specific stuff, then do your checks and stuff and finally either call submit or return false.

Kaze no Koe
I see your point, but using the event attributes is the simplest form of coding JS, that's why most people use it.
DisgruntledGoat