Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Required checkbox

New Here ,
Aug 18, 2009 Aug 18, 2009

I have a coldfusion form with <cfform> . I have 3 check boxes in the form that I need to make sure at least one of them has checked. How  can I do that.

I really appreciate if somebody can help me.

Thanks.

3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 18, 2009 Aug 18, 2009

may be JavaScript is the answer.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 18, 2009 Aug 18, 2009

There are many ways to do it. But it all really depends on what form validation you're using now.

You could do something like:

<cfif IsDefined("form.submit")>

     <cfif NOT IsDefined("form.checkbox1") AND NOT IsDefined("form.checkbox2") AND NOT IsDefined("form.checkbox3")>

          code to process form as error

     </cfif>

</cfif>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 19, 2009 Aug 19, 2009

I googled 'javascript required checkbox' and found some good suggestions. Here's a simple one that looks good:

<SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT>

function checkCheckBoxes() {
     if (document.frmTest.CHKBOX_1.checked == false &&
         document.frmTest.CHKBOX_2.checked == false &&
         document.frmTest.CHKBOX_3.checked == false)
          {
          alert ('You didn\'t choose any of the checkboxes!');
          return false;
          }
     else
          {
          return true;
          }
     }
//-->
</SCRIPT>



<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" name="CHKBOX_1" value="1">1</p>
<input type="checkbox" name="CHKBOX_2" value="2">2</p>
<input type="checkbox" name="CHKBOX_3" value="3">3</p>
<input type="submit" value="Submit!" />
</form>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2009 Aug 19, 2009

That only works when each checkbox has a different name.  It's often the case that they all have the same name.  In this case, the ever popular GetElementById comes into play.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2009 Aug 27, 2009
LATEST

Thanks it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 19, 2009 Aug 19, 2009

You can use JavaScript, server-side Cold Fusion code, or better yet, both. Remember, though, that JavaScript is only for user-friendly type of validation.In almost all cases, you really need to use server-side validation, because users can circumvent JavaScript. You can do the server-side validation in several ways, the code in the reply from gedecus will work well enough.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 20, 2009 Aug 20, 2009

Provided that all three checkboxes have the same name, just use the built in cf validation ;

<cfinput type="checkbox" name="cars" value="Honda" required="yes" message="Please check at least one car">Honda <br>

<cfinput type="checkbox" name="cars" value="Ford"> Ford <br>

<cfinput type="chekcbox" name="cars" value="Toyota">Toyota

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources