Checkbox field validating radio button....?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rush it
    New Member
    • Oct 2011
    • 18

    Checkbox field validating radio button....?

    Is it possible to have a checkbox validate a radio button? I have two radio buttons (Free Lunch and Reduced Lunch). If the user chooses either of these, the checkbox below (Please agree to the release of school information about your lunch program...) must be checked. This checkbox ONLY needs to be selected IF one of the radio buttons is selected. So far I'm trying to do this in javascript.

    Here's what I have now that is not working. When testing the form, I'm selecting a radio button, and not selecting the checkbox and the form submits anyway.

    Code:
    function validateLunchSelection()
    
     if((form2.lunch_program[0].checked==true || form2.lunch_program[1].checked==true) && (form2.lunch_auth.checked==false))
    {
    alert("Please agree to release of lunch information from your school");
    form2.lunch_auth.focus();
    return false;
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    how do you (try to) prevent the form from submitting?

    Comment

    • rush it
      New Member
      • Oct 2011
      • 18

      #3
      Sorry, but that's what I'm having problems with. I'm using javascript validation for the other fields, but can't figure out how to associate these two fields. Guess I'm not sure what you're asking.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        in general there are three major ways of submit and validate a form
        - preventing the submit action of the form itself (if validation failed)
        - preventing the submit button from submitting (if validation failed)
        - not using a submit button (but a click button) and submit (or not) the form via JavaScript

        there are various ways to implement each way, depending in the version of IE that has to be supported

        Comment

        • rush it
          New Member
          • Oct 2011
          • 18

          #5
          I'm preventing submission by the submit button, the second item you listed (when validation fails). We're concerned with supporting IE 7, 8, and 9. Thanks for your time.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            all you need to do is stopping the button from executing its default action. as of IE 9 this is done via Event.preventDe fault() and if I remember right previous versions of IE use event.returnVal ue = false;

            Comment

            • rush it
              New Member
              • Oct 2011
              • 18

              #7
              Thanks, that will keep the from from submitting when it shouldn't, but do you have experience with my first question about a checkbox field validating a radio button?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                on click on either radio button trigger a function that
                - tests if either redio button is checked
                - tests if the checkbox is checked or set the checkbox to checked

                Comment

                • rush it
                  New Member
                  • Oct 2011
                  • 18

                  #9
                  I don't need to validate the radio buttons and checkbox separately, but together. If either radio button is selected, the checkbox MUST be selected. All three deal with two possible answers to a question (radio buttons), then the checkbox is a legal release of information. If this can't be done in javascript, then I'll validate them a more standard way. I'm not even sure if it CAN be done. The syntax I've tried isn't working. Thanks.

                  Comment

                  Working...