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>

________________________________________________________________________________________________
tinglewebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

callrsCommented:
if(document.formname.checkboxname.checked) alert("checkbox is checked")
else alert("checkbox is not checked')
callrsCommented:
<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>                    
AmigoJackCommented:
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
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

tinglewebAuthor Commented:
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.

callrsCommented:
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>                    

tinglewebAuthor Commented:
With the code above callrs it doesnt actually prevent me submitting the form and moving to the next page if the checkbox is unchecked.
callrsCommented:
Sorry. Asleep at the wheel in this dark basement. Need sunlight! :-p

A true here, a false there, and Voilà

<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"); return false;}
}
</script>

<form onsubmit='return 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>                    

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.