views:

187

answers:

1

HI, I am trying to validate a radio button form that has an unknown amount of radio groups.

So far I have this:

var object = $(".radio:checked");
var length = $('#numq').val();
if(object.length==length) {
    return true;
} else {    
    var unchecked = $(".radio:not(:checked)").empty();
    var q = "";
    unchecked.each(function(){
        q = q+$(this).parent("div").parent("li").children("div.question").children("b").text()+(",");
    });
    alert("Please answer question "+q);
    return false;
}

Which will check if all groups are checked fine... but when it comes to finding out the actual box that has been missed out, I'm having issues.

The markup for my radio buttons (php) is below:

<div class="question"><b><?=$qnum?></b><span><?=$row['question']?></span></div>

<div class="a1 answer">
    <input type="radio" class="radio" name="<?=$row['id']?>" value="1">
    <label><?=$row['answer1']?></label>
</div>

<div class="a2 answer">
    <input type="radio" class="radio" name="<?=$row['id']?>" value="2">
    <label><?=$row['answer2']?></label>
</div>

<div class="a3 answer">
    <input type="radio" class="radio" name="<?=$row['id']?>" value="3">
    <label><?=$row['answer3']?></label>
</div>

<div class="a4 answer">
    <input type="radio" class="radio" name="<?=$row['id']?>" value="4">
    <label><?=$row['answer4']?></label>
</div>

Any ideas? Thanks!

A: 

In your loop where radio are generated, add a variable to count them, put total count in a hidden field, then from script read the value of this hidden field using getelementbyid and run the loop for as much time as this total and validate accordingly.

This way, you know total number of them and validation process becomes easier then.

Sarfraz
not sure what you mean... i already have a hidden field on the page that is the number of questions - im not sure how i can use this to manipulate my validation :/
tarnfeld