[Last Call] Learn how to a build a cloud-first strategyRegister Now


Validation: If radiobutton is checked, at least 1 checkbox is checked... and...

Posted on 2006-05-20
Medium Priority
Last Modified: 2010-08-05
Ok experts.. I have 2 radiobuttons. A and B.
If radiobutton A is checked, then at least 1 checkbox need to be checked.
There are a list of checkboxes, all of them with the same name just different values. So it will be validating an array.
On some of the checkboxes option, there's text fields.
So when a particular checkbox is checked, the field also need to have a value. I'll try to draw it out below.

I don't know if this add into the complexity, but I wrote it so that only when radiobutton A is checked, the options are shown, by toggling between css style hidden / not.

o = radiobutton
x = checkboxes
___ = textfields

o Radiobutton A  o Radiobutton B
x option 1
x option 2
x option 3 text field _______
x option 4
.. the options goes on.
Question by:dellon
LVL 17

Accepted Solution

Cem Türk earned 700 total points
ID: 16726259

<script type="text/javascript">
function validate(f) {
var chkz=document.getElementsByName("chk");
var rtn=false;
if(document.getElementById('opts').style.display=="block") {
for(i=0;i<chkz.length;i++) {
if(chkz[i].checked==true) {
if (chkz[i].checked==true && f.opttxt.value=="" && chkz[i].value=="3"){
alert("Please type info for option 3");
return false;

if(rtn==false) {
alert("Please select an option");

return rtn;

<form name="myform" onsubmit="return validate(this)">
<input type="radio" name="AB" onclick="document.getElementById('opts').style.display='block'" id="A" value="A">Radio A <input

type="radio" name="AB" onclick="document.getElementById('opts').style.display='none'" id="B" value="B">Radio B
<div id="opts" style="display:none" name="opts">
<input type="checkbox" value="1" id="chk" name="chk">Option 1<br>
<input type="checkbox" value="2" id="chk" name="chk">Option 2<br>
<input type="checkbox" value="3" id="chk" name="chk">Option 3<input type="text" name="opttxt" id="opttxt" value=""><br>
<input type="checkbox" value="4" id="chk" name="chk">Option 4<br>
<input type="submit" value="Send">

Author Comment

ID: 16770321
Sorry been busy with CRM upgrade.
Your script worked like a charm. Thanks!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question