4

I got a probably very silly question, but I couldn`t find it on the website anywhere.

I am making a website for an assignment and I`m doing rather well, but I am having trouble with the Terms and conditions radio buttons.

What I have is basically 2 radio buttons, one is Agree, second is disagree. The Disagree button is pre-selected. What is supposed to happen is basically a check if the terms and conditions are agreed before continuing to the next page.

TERMS & CONDITIONS

I have read and understood the CQU Privacy and Security Statement and agree to the use of my personal details for related activities.

<input type="radio" name="radio" id="yes">Yes, I agree.
<input type="radio" name="radio" id="no" checked="checked">No, I don`t agree.<br>

<form action="endPage.jsp">
    <input type="submit" value="Submit" name="submit" />
</form>

So basically what I need is to get the user to agree to terms and conditions, otherwise give a popup window saying you have to agree with terms and conditions.

Thank you

tl;dr - How do I check if a certain checkbox is checked before submitting a form?

Barrie Reader
  • 10,647
  • 11
  • 71
  • 139
user2706317
  • 41
  • 1
  • 2
  • @Teemu - what do you mean you don`t know my question? The question is, what do I need to do to make the user select the agree button before continuing? – user2706317 Aug 22 '13 at 07:52
  • @user2706317 The previous comment of mine was just a different way to ask you, what have you tried? Or: SO is not a free programming service... – Teemu Aug 22 '13 at 07:58
  • @Teemu - I haven`t tried anything yet because I can`t find anything similar on the internet... All I could find so far is how to make someone select a radio button if non are selected, but then again it doesn`t say that it has to be any specific button, but any of the ones offered... I already have a radio button selected and I need to make the user select the agree button before I can let him continue... Hope that gives some explanation... – user2706317 Aug 22 '13 at 08:04
  • @user2706317 - I answered a while ago... – Barrie Reader Aug 22 '13 at 08:06
  • @Neurofluxation - Tnx m8, I tried it, but doesn`t work, I still get through every time... I`ll try the other code now.. – user2706317 Aug 22 '13 at 08:11

2 Answers2

3

Standard Javascript would be something like this... I imagine...

var submitBtn = document.getElementById("submit");
var termsChk = document.getElementById("yes");
var formFrm = document.getElementById("form");

submitBtn.addEventListener("click", function() {
    if (termsChk.checked === true) {
        alert("Checked!");
        formFrm.submit();
    } else {
        alert("Not Checked!");
    }
    return false;
});

Quick Fiddle: http://jsfiddle.net/neuroflux/yrwMc/6/

With jQuery:

$('#submitBtn').on('click', function() {
    if ($('#yes').attr('checked') === "checked") {
        alert("Checked!");
        $('#form').submit();
    } else {
        alert("Not Checked!");
    }
    return false;
});

Fiddle: http://jsfiddle.net/neuroflux/yrwMc/8/

Barrie Reader
  • 10,647
  • 11
  • 71
  • 139
0
<script type="text/javascript">
function fnCheckRadio()
{   
     var radYes=document.getElementById("yes"); 
     if(radYes.checked)
     {              
        return true;
     }
     else
     {
        alert("Please agree Terms and condition");
        return false;
     }
   return false;
}

</script>

<form action="endPage.jsp">
    <input type="submit" value="Submit" name="submit" onclick="return fnCheckRadio();" />
</form>
Pradeep
  • 1
  • 1