Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,772 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,350 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



CFFORM multiple checkbox - how to require ONE or more

 
Reply to this topicStart new topic

CFFORM multiple checkbox - how to require ONE or more, Can't leave them blank - at least one has to be checked

rickdrew
post 10 May, 2008 - 10:09 PM
Post #1


New D.I.C Head

*
Joined: 10 May, 2008
Posts: 6


My Contributions


I have a form with sever fields and some checkboxes. The form needs at least one to be checked. I use CF's builtin error checking on the text fields so I don't have to error trap on the form action, but I can't see how to do that with different check boxes.

Yes, I can check it in the action, but that's a little sloppy since the other errors display in a popup as soon as they hit SUBMIT.

Thanks

Rick

This post has been edited by rickdrew: 10 May, 2008 - 10:10 PM
User is offlineProfile CardPM

Go to the top of the page


sansclue
post 11 May, 2008 - 12:17 PM
Post #2


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 66



Thanked 2 times
My Contributions


QUOTE(rickdrew @ 10 May, 2008 - 10:09 PM) *

I have a form with sever fields and some checkboxes. The form needs at least one to be checked. I use CF's builtin error checking on the text fields so I don't have to error trap on the form action, but I can't see how to do that with different check boxes.


Do the checkboxes have the same name or different names?

QUOTE(rickdrew @ 10 May, 2008 - 10:09 PM) *

Yes, I can check it in the action, but that's a little sloppy since the other errors display in a popup as soon as they hit SUBMIT.


Unless it is a flash form, I do both. Otherwise, if the user disables javascript ... there goes all the validation ;-)
User is offlineProfile CardPM

Go to the top of the page

sansclue
post 11 May, 2008 - 01:46 PM
Post #3


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 66



Thanked 2 times
My Contributions


Assuming it is an html form, and that your checkboxes do have different names, try using "onValidate". It calls a javascript function (that you code yourself) when the form is submitted. The function should return true or false. ie true if at least one box was checked, otherwise false. If it returns false, an error message is displayed and the form will not be submitted.

Here is the basic structure.

CODE

<script type="text/javascript">

        // the function must accept these parameters or it will not work
    function verifyCheckedBoxes(objForm, objField, fieldValue) {

        // your javascript code that counts the total
        // number of checked boxes here


        //  if at least one box was checked, return true. Otherwise, return false here
        return true or false here;
    }
</script>

<cfform action="theActionPage.cfm" method="post">
    Option A <cfinput type="checkbox" name="option_A" value="A" onValidate="verifyCheckedBoxes" message="Check at least one box"><br>
    Option B <cfinput type="checkbox" name="option_B" value="B"><br>
    Option C <cfinput type="checkbox" name="option_C" value="C"><br>
    
    <cfinput type="submit" name="submitThis">
</cfform>
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 04:00PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month