Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
may be JavaScript is the answer.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks it works.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more