0

I'm trying to validate radio buttons in JavaScript and this is my code:

if (document.ExamEntry.GCSE.checked == true) {
    confirm("You have selected GCSE. Is this correct?");
}
if (document.ExamEntry.AS.checked == true) {
    confirm("You have selected AS. Is this correct?");
}
if (document.ExamEntry.A2.checked == true) {
    confirm("You have selected A2. Is this correct?");
}

The confirm window shows up and clicking 'Submit' successfully takes you to the next page, however the cancel button doesn't seem to work - it just takes you to the next page when I want it to stay on the page so you can change your option.

I've tried things such as return result; result = false

They either don't work, or if they do, then it's vice versa so that the cancel button works by staying on the same page, but this will happen with the submit button too.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 2
    What next page? Are you talking about the next confirm box, or are you redirecting somewhere ? – adeneo Dec 11 '13 at 21:53
  • 3
    Your confirms don't do anything with the result of the confirm. They might as well be alerts. – Kevin B Dec 11 '13 at 21:53
  • You'll want to keep track of the result of the confirm and do something with it... – Brian Driscoll Dec 11 '13 at 21:54
  • Show us more of your code. – SunSparc Dec 11 '13 at 21:54
  • you must use if(confirm("You have selected GCSE. Is this correct?")) { //do something } – vrunoa Dec 11 '13 at 21:54
  • Apologies, it seems I may have been too vague. The submit redirects it to a different page, but the cancel seems to do the same but I'm wanting the cancel button to make sure that the user stays on the page they're on, not redirect them like the submit button does. – user3093021 Dec 11 '13 at 21:56
  • Think you need to supply a little more code. Particularly helpful would be the event handler that runs this code in response to user input. – Captain John Dec 11 '13 at 21:56

3 Answers3

3

Check out the documentation on confirm. It says,

result is a boolean value indicating whether OK or Cancel was selected (true means OK)

That means each of your lines should check the return value. A concise way to do this is, e.g.:

if (!confirm("You have selected A2. Is this correct?")) {
    // event.cancel = true, or whatever you need to do on your side to cancel
} // otherwise fall through and do what you're doing.

As it is right now, since you never look at the return value of confirm, so you always fall through to your "success" case.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
0
var gcse = true, 
    as   = true,
    a2   = true;

if (document.ExamEntry.GCSE.checked == true) {
    gcse = confirm("You have selected GCSE. Is this correct?"));
}

if (document.ExamEntry.AS.checked == true) {
    as = confirm("You have selected AS. Is this correct?");
}

if (document.ExamEntry.A2.checked == true) {
    a2 = confirm("You have selected A2. Is this correct?");
}

if (gcse && as && a2) {
    // you're golden
    window.location.href = 'otherpage'
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
0
if (document.ExamEntry.GCSE.checked == true) { 
    if(confirm("You have selected GCSE. Is this correct?")) {
        // do something
    }
} if (document.ExamEntry.AS.checked == true) {
    if(confirm("You have selected AS. Is this correct?")) {
        // do something
    }
}  
if (document.ExamEntry.A2.checked == true) {
    if(confirm("You have selected A2. Is this correct?")) {
        //do something
    }
}
vrunoa
  • 766
  • 8
  • 15