Link to home
Start Free TrialLog in
Avatar of tingleweb
tinglewebFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How make sure a checkbox is checked before form submit?

Hi

I am using the code from http://www.javascript-coder.com/html-form/javascript-form-validation.phtml in my form:

here is an extract of the code - unfortunately whether the checkbox (termsagreed) is checked or not - I still get the 'required fields not completed' popup.  How can I only get it to appear if the form is submitted with termsagreed unchecked?

________________________________________________________________________________________________

<label for="terms-agreed" id="terms">I have read and agree with the Terms &amp; Conditions*</label>
<input name="termsagreed" type="checkbox" id="termsagreed" value="" class="termsCheck" />

<div id="detailsButtons2"><input type="image" src="images/but-continue.gif" alt="continue" id="but3" /></div>      

</fieldset>                        
</form>                        

<script language="JavaScript" type="text/javascript">
 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("termsagreed","req","Please read and agree with the Terms & Conditions");
</script>

________________________________________________________________________________________________
Avatar of callrs
callrs

if(document.formname.checkboxname.checked) alert("checkbox is checked")
else alert("checkbox is not checked')
<script language="JavaScript" type="text/javascript">
function validate(obj){
var c=obj.form.termsagreed;
if(c.checked) alert("Checked!")
}
</script>

<form>
<label for="terms-agreed" id="terms">I have read and agree with the Terms &amp; Conditions*</label>
<input name="termsagreed" type="checkbox" id="termsagreed" value="" class="termsCheck" onClick='validate(this)'/>

<div id="detailsButtons2"><input type="image" src="images/but-continue.gif" alt="continue" id="but3" /></div>    

</fieldset>                    
</form>                    
good one :) and for those who have javascript disabled? a better way would be to send it to the server. with a serverside language like php or else you have more chances of control
Avatar of tingleweb

ASKER

callrs, that doesnt really help to be honest...

I can submit the form whether the checkbox is checked or not with your solution.
If I click the checkbox I get an alert saying 'checked!'

I need it to alert a message/prevent the form being submitted if a person tries to submit the form without checking the box first.

Code above showed how to check if a box is checked. Ad an if/else etc. for what you need:

<script language="JavaScript" type="text/javascript">
function validate(frm){
var c=frm.termsagreed;
if(c.checked) return true;
else alert("Please read and agree with the Terms & Conditions")
}
</script>

<form onsubmit='validate(this)'>
<label for="terms-agreed" id="terms">I have read and agree with the Terms &amp; Conditions*</label>
<input name="termsagreed" type="checkbox" id="termsagreed" value="" class="termsCheck"/>
<input type='submit' value='Submit'>
<div id="detailsButtons2"><input type="image" src="images/but-continue.gif" alt="continue" id="but3" /></div>    

</fieldset>                    
</form>                    

With the code above callrs it doesnt actually prevent me submitting the form and moving to the next page if the checkbox is unchecked.
ASKER CERTIFIED SOLUTION
Avatar of callrs
callrs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial