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

Posted on 2006-05-20
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


    <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

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

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    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…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now