Web Analytics Made Easy -
StatCounter how to validate if two radio buttons are checked - CodingForum

Announcement

Collapse
No announcement yet.

how to validate if two radio buttons are checked

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to validate if two radio buttons are checked

    Hi! I hope someone can help me:

    I have 3 groups of radio buttons (each group has more than 3 choices)

    A - full day course (3 choices)
    B - half day morning (4 choices)
    C - half day afternoon (4 choices)

    I need to
    1) alert users only when any A and any C are BOTH selected (schedule conflict)

    2) alert users only when any B and any C are BOTH selected (qualifies for discount)

    I've tried

    function validate(form1) {
    if(document.form1.workshop[0].checked)
    {
    alert('You must choose an option, and complete the form field');
    }
    }

    This works, but only when I click on the first choice of group A.
    So I tried to create a variation to validate when A and C are both selected:
    if(document.form1.workshop[0].checked && if(document.form1.halfday[0].checked)
    but it didn't work...

    plus, I don't want to detect just workshop[0], but any of the 3 choices (workshop0-2)

    any help will be greatly appreciated!!!
    Thanks a lot!!!!

    YC

  • #2
    Please post the HTML code.
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      here is the form

      <form name="form1" method="post" onSubmit="return validate(this);" action="">
      Full Day Workshop
      <input type="radio" name="workshop" value="W1">FullW1
      <input type="radio" name="workshop" value="W2">FullW2
      <input type="radio" name="workshop" value="W3">FullW3
      <input type="radio" name="workshop" value="W4">HalfW4<br><br>
      Half day workshop
      <input type="radio" name="halfwork" value="W5">HalfW5
      <input type="radio" name="halfwork" value="W6">HalfW6
      <input type="radio" name="halfwork" value="W7">HalfW7<br><br>
      Half day tutorial
      <input type="radio" name="halftutor" value="T1">halfT1
      <input type="radio" name="halftutor" value="T2">halfT2
      <input type="radio" name="halftutor" value="T3">halfT3
      <input type="radio" name="halftutor" value="T4">halfT4<br>
      <input type="submit" name="Submit" value="Submit">
      </form>

      How do I detect when
      - any 1 Full day workshop and any 1 half day workshop are both selected (schedule conflict)
      or
      - any 1 full day workshop and any 1 half day tutorial are both selected
      (schedule conflict)
      or
      - any 1 half day workshop and any 1 half day tutorial are both selected
      (qualify for discount)

      If someone can show me at least how one condition works, it will be greatly appreciated.

      YC

      Comment


      • #4
        Use this function to check if any item is checked in a group of radios.
        Code:
        function detectCheck(radGrp){
          for (var j=0;j<radGrp.length;j++){
             if (radGrp[j].checked) return true; //one of them is checked
          }
          return false; //none is checked
        }
        for example:
        Code:
        function validate(f){
          //condition 1
          if (detectCheck(f.workshop) && detectCheck(f.halfwork)){
             alert("Schedule conflict!");
             return false;
          }
        }
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          Dear glenngv:

          It works!!!!

          Thank you thank you thank you!!!

          You really make my day!!!!

          Truly appreciate your help!!!!!

          Comment


          • #6
            Glad I was able to help you.
            Glenn
            vBulletin Mods That Rock!

            Comment

            Working...
            X