Avatar of tingleweb
tingleweb
Flag 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>

________________________________________________________________________________________________
JavaScript

Avatar of undefined
Last Comment
callrs

8/22/2022 - Mon
callrs

if(document.formname.checkboxname.checked) alert("checkbox is checked")
else alert("checkbox is not checked')
callrs

<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>                    
AmigoJack

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
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.

callrs

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>                    

tingleweb

ASKER
With the code above callrs it doesnt actually prevent me submitting the form and moving to the next page if the checkbox is unchecked.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
callrs

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question