Skip to content
Advertisement

Check if at least one checkbox is selected PER questio.n

Here we go again , searched and tried myself but none of the online examples are not the same as in my case. I have 2 radio based question and other 22 with checkboxes. My question is how to check if at least 1 checkbox/radio is selected on each question and display javascript alert in case they are not EG : “You have to select at least one answer on question number 3 “.

Note: The alert must contain question number . The CHECK MUST be done on form submit (if it fail it will return false , otherwise true and it will be processed.)

Example below:

<tr><th>- Question 1</th></tr>
<tr><td><input name="01" type="radio" id="01" value="F">
F</td></tr>
<tr><td><input name="01" type="radio" id="01" value="B">
B</td></tr>

<tr><th>- Question 2</th></tr>
<tr><td><input name="02" type="radio" id="02" value="9">
9</td></tr>
<tr><td><input name="02" type="radio" id="02" value="10">
10</td></tr>
<tr><td><input name="02" type="radio" id="02" value="11">
11</td></tr>
<tr><td><input name="02" type="radio" id="02" value="12">
12</td></tr>

<tr><th> Question 3</tr></th>
<tr><td><input name="c1" type="checkbox" id="c1" value="a">
This is checkbox1</td></tr>
<tr><td><input name="c1" type="checkbox" id="c1" value="b">
This is checkbox2</td></tr>

Advertisement

Answer

EDIT – edited after seeing the markup

$('form').submit(function() {
    var ok = true;
    var that = this;
    //get the <tr> inside the function, i assume thatyou have one question per <tr>
    $('tr th', this).each(function(idx, el) {
        var id = this.id;
        if (id) {
            if ($('input:checked[name=' + id + ']', that).length === 0) {
                alert("You must check at least one option for question " + $(this).text());
                ok = false;
            }
        }
    });
    if (ok === false) {
        return false;
    }
});

This is the markup i used (fiddle here http://jsfiddle.net/R7uwa/2/)

<form id="chestionar" name="chestionar" method="post" action="prelucrare_chest.php">

    <table style="width:100%; border-spacing:0;">
    <tbody><tr><th>- Selectati sex-ul:</th></tr>
    <tr><td><input type="radio" value="F" id="01" name="01">
    Fata</td></tr>
    <tr><td><input type="radio" value="B" id="01" name="01">
    Baiat</td></tr>

    <tr><th>- Selectati clasa:</th></tr>
    <tr><td><input type="radio" value="9" id="02" name="02">
    9</td></tr>
    <tr><td><input type="radio" value="10" id="02" name="02">
    10</td></tr>
    <tr><td><input type="radio" value="11" id="02" name="02">
    11</td></tr>
    <tr><td><input type="radio" value="12" id="02" name="02">
    12</td></tr>

    <tr><th id="c1">1. Care sunt motivele pentru care absentezi tu si colegii tai?</th></tr>
    <tr><td><input type="checkbox" value="a" id="c1" name="c1">
    Nu ne place ora</td></tr>
    <tr><td><input type="checkbox" value="b" id="c1" name="c1">
    Nu ne intereseaza scoala</td></tr>
    <tr><td><input type="checkbox" value="c" id="c1" name="c1">
    Nu ne place profesorul</td></tr>
    <tr><td><input type="checkbox" value="d" id="c1" name="c1">
    Daca absentam nu ni se întâmpla mare lucru</td></tr>
    <tr><td><input type="checkbox" value="e" id="c1" name="c1">
    Ne plictisim la scoala</td></tr>
    <tr><td><input type="checkbox" value="f" id="c1" name="c1">
    Facem usor rost de scutiri pentru motivarea absentelor</td></tr>
    <tr><td><input type="checkbox" value="g" id="c1" name="c1">
    Evitam în felul acesta luarea unei note mici</td></tr>
    <tr><td><input type="checkbox" value="h" id="c1" name="c1">
    Nu dorim sa intram în conflict cu ceilalti colegi</td></tr>
    <tr><td><input type="checkbox" value="i" id="c1" name="c1">
    Programul este prea încarcat</td></tr>



    <tr><th id='c2'>2. Ce se întâmpla când un elev absenteaza nemotivat mai mult timp?</th></tr>
    <tr><td><input type="checkbox" value="a" id="c2" name="c2">
    I se scade nota la purtare</td></tr>
    <tr><td><input type="checkbox" value="b" id="c2" name="c2">
    Sunt chemati parintii lui la scoala</td></tr>
    <tr><td><input type="checkbox" value="c" id="c2" name="c2">
    Este mustrat de catre diriginte</td></tr>
    <tr><td><input type="checkbox" value="d" id="c2" name="c2">
    Este evaluat la ora urmatoare si este depunctat la nota</td></tr>
    <tr><td><input type="checkbox" value="e" id="c2" name="c2">
    Este exmatriculat</td></tr>
    <tr><td><input type="checkbox" value="f" id="c2" name="c2">
    Nu se întâmpla mare lucru</td></tr>

    <tr><th id='c3'>3. Cât de des absentezi nemotivat?</th></tr>
    <tr><td><input type="checkbox" value="a" id="3" name="3">
    Deloc</td></tr>
    <tr><td><input type="checkbox" value="b" id="c3" name="c3">
    1-3 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="c" id="c3" name="c3">
    3-6 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="d" id="c3" name="c3">
    6-9 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="e" id="c3" name="c3">
    9-12 ore pe saptamânat</td></tr>
    <tr><td><input type="checkbox" value="f" id="c3" name="c3">
    12-15 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="g" id="c3" name="c3">
    Peste 15 ore pe saptamâna</td></tr>
    </tbody></table>
    <div class="form_settings"><p style="padding-top: 15px"><span>&nbsp;</span><input type="submit" value="Trimite" name="contact_submitted" class="submit"></p></div></form>
Advertisement