Skip to main content
Inspiring
October 16, 2012
Question

Require question with checkboxes to be answered

  • October 16, 2012
  • 2 replies
  • 1578 views

Hi.

    I have some questions on a form and I want to require that they all be answered with something. I have some Cold Fusion code that puts a requirement on some of the questions, so if they don't answer it and they click submit, a pop up will display telling them they have to answer question 3 for example. Here's waht I have on one of the questions that has radio buttons:

<cfinput type="radio" name="Current_Project" value="yes" required="yes" message="Please answer question 2.">Yes  

  <cfinput type="radio" name="Current_Project" value="no">No

   How can I do this on a question that has checkboxes since multiple ones can be checked? Each checkbox is a different name also. That's the problem. If they were all the same, it would be easy to code this. But they each need to be a seperate name because that's how I have them in the database. If I put the require code on each checkbox, the form will not go through until all the checkboxes are checked. I don't want that becasue the user needs to be able to choose their choices for that question and maybe only have 1 or 2 answers. Can I do this with Cold Fusion, or do I need to do javascript with these? Does anyone know how to do this? Thank you.

Andy

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    October 17, 2012

    jamie61880 wrote:

       How can I do this on a question that has checkboxes since multiple ones can be checked? Each checkbox is a different name also. That's the problem. If they were all the same, it would be easy to code this. But they each need to be a seperate name because that's how I have them in the database. If I put the require code on each checkbox, the form will not go through until all the checkboxes are checked.

    I see no problem. Take the following example. The form will definitely go through if just one box (the required one) is checked.

    <cfif isDefined("form.fieldnames")>

    <cfdump var="#form#">

    </cfif>

    <cfform>

    <cfinput type="Checkbox" name="temperature" value="1">Temperature<br>

    <cfinput type="Checkbox" name="humidity" value="1">Humidity<br>

    <cfinput type="Checkbox" name="vibration" value="1" required="yes" message="You must check the Vibration box.">Vibration<br>

    <cfinput type="submit" name="sbmt" value="send">

    </cfform>

    Inspiring
    October 16, 2012

    Why does each checkbox have it's own name?  Are you able to change it so that each set of answers to a particular question has the same name? 

    No matter what the names, client side validation has to happen with js, not cf.

    Inspiring
    October 16, 2012

    The reason each checkbox has it's own name is so that I can tally up the answers for each check box later and put it into a graph format so we can see how many people clicked on that checkbox. For instance, here's a question we have with checkboxes:

    9. Please select your enviornmental requirements and specify.

    _ Temperature

    _ Humidity

    _ Vibration

    Then in the database I have 3 columns like this and the answer is a yes or no for each person that fill out the info.:

    Name     Temperature     Humidity     Vibration

    Andy         Yes                    No              No

    Mike          No                    No              Yes

    John          Yes                   Yes             Yes

    This way, I can add up how many people checked off Temperature which is 2. Humidity has 1 and Vibration has 2. If they were all under one column called something like Environmental_Requirements, the values would all have to be in the same cell for each person, correct? Like this:

    Name     Environmental_Requirements

    Andy         Temperature

    Mike          Vibration

    John          Temperature, Humidity, Vibration

        Wouldn't this be harder to run a query on this to add up all the ones that have Temperature in this column for instance? How else should this be set up? This is only for 1 question. I have 8 other checkbox questions the user can choose multiple things for on each question.

         Does the Validation with Cold Fusion only work on the radio buttons and text boxes then and not the checkboxes when there are multiple checkboxes for 1 question?

    Andy

    Inspiring
    October 16, 2012

    One thing at a time.  For this question:

    9. Please select your enviornmental requirements and specify.

    _ Temperature

    _ Humidity

    _ Vibration

    What are the names of the checkboxes and, once the form has been submitted, how do you determine which boxes were selected?