Cheok dee
Cheok dee

Reputation: 39

Loop through and validate multiple radio inputs

JS:

function validateForm() {
    var radios = document.getElementsByName("Dquestion[1]");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) formValid = true;
        i++;        
    }

    if (!formValid) alert("Must check some option!");
    return formValid;
}

PHP:

<form name="form1" action="#" onsubmit="return validateForm()" method="post"> 
    First time visitor?:<br/>
        <label for="s1">Yes</label>
        <?php for($i=1; $i<=10; $i++){?><td><input type="radio" name="Dquestion[1]" value="<?=$i?>"> <?=$i?> </td><?php } ?>
        <br/>
        <label for="s2">No</label>
        <?php for($i=1; $i<=10; $i++){?><td><input type="radio" name="Iquestion[1]" value="<?=$i?>"> <?=$i?> </td><?php } ?>
        <br/>
         <label for="s3">cool</label>
        <?php for($i=1; $i<=10; $i++){?><td><input type="radio" name="Dquestion[2]" value="<?=$i?>"> <?=$i?> </td><?php } ?>
        <br/> 
    <input type="submit" value="Submit"><br/>
</form>

My intention is to make all inputs checked, and if not, they can't submit the form. I have 10 radio inputs for each question, and the form has 25 questions, so I use Dquestion['number'] for each name. I am using this code to test but the Dquestion[2] can't be checked. Why? How am I going to loop all 25 questions?

I tried this code, but it didn't work:

var Dnames = ['Dquestion[1]','Dquestion[2]']

function validateForm() {

    var radios = [];
    var formValid = false;

    for (var i = 1; i<= Dnames.length; i++){
        var radios = document.getElementsByName(Dnames[i]);
        var j = 0;
        while(!formValid && j < radios.length){
            if(radios[j].checked) formValid = true;
            j++
        }
    }

    if (!formValid) alert("Must check some option!");
    return formValid;
}

Upvotes: 0

Views: 1439

Answers (2)

Marventus
Marventus

Reputation: 874

I would wrap each set of radio buttons pertaining to a particular question around a div and check each div to see if at least one radio button is checked.

PHP:

<form id="form1" name="form1" action="#" method="post" onsubmit="return validateForm();"> 
    <p>First time visitor?:</p>
    <div class="question">
        <label for="s1">Yes</label>
        <?php for($i=1; $i<=10; $i++){?><input type="radio" value="<?=$i?>"> <?=$i?> <?php } ?>
    </div>
            <br/>
    <div class="question">
        <label for="s2">No</label>
        <?php for($i=1; $i<=10; $i++){?><input type="radio" value="<?=$i?>"> <?=$i?> <?php } ?>
    </div>
            <br/>
    <div class="question">
         <label for="s3">Cool</label>
        <?php for($i=1; $i<=10; $i++){?><input type="radio" value="<?=$i?>"> <?=$i?> <?php } ?>
    </div>
        <br/> 
    <input type="submit" value="Submit"><br/>
</form>

JS:

function validateForm() {
        var questions = document.getElementsByClassName("question"),
            formValid = true;
        for( var j=0; j<questions.length; j++ ) {
            if( !isOneInputChecked(questions[j], "radio") ) {
                formValid = false;
            }
        }
        alert(formValid ? "Submission Succesfull!" : "Submission Failed!");
        return formValid;
    }
    function isOneInputChecked(sel) {
        // All <input> tags...
        var inputs = sel.getElementsByTagName('input');
        for (var k=0; k<inputs.length; k++) {
            // If you have more than one radio group, also check the name attribute
            // for the one you want as in && chx[i].name == 'choose'
            // Return true from the function on first match of a checked item
            if( inputs[k].checked )
                return true;
        }
        // End of the loop, return false
        return false;
    }

JSFiddle:

Here is a working JSFiddle for reference.

Credit:

isOneInputChecked() function taken from Michael Berkowski's reply in a different thread (link).

Upvotes: 0

Edwin Reynoso
Edwin Reynoso

Reputation: 1541

var radios = document.getElementsByName('Dquestion[1]'); // you understand this part
var checked = Array.prototype.some.call(radios, function(radio){
    return radio.checked;
});

Let's say I have an array:

var arr = [1,2,3,4,5,6];
arr.some(function(num){
 return boolean // meaning return something that is true or false like "return num < 3
});
//when it loops through it will return that boolean each time
// it'll return 1 < 3: true, 2 < 3: true 3 < 3: false, 4 < 3: false, 5 < 3: false, 6 < 3: false
//the some function after it's done looping is checking if some are true which is true it returned 3 trues
//so checked = true

if(checked){
    console.log('one is checked');
} else {
    console.log('please check one');
};

Upvotes: 2

Related Questions