php - How to check this form properly filled out in an easier way? -


i have questionnaire of 1 question offers 5 answer options. user may tick three, , give 3 rating. avoid misunderstanding, here html code:

<table>     <tr>         <td><input type="checkbox" name="option_1"> option 1 (to determined)</td>         <td>             <select name="">                 <option value="">as:</option>                 <option value="3">1st</option>                 <option value="2">2nd</option>                 <option value="1">3rd</option>             </select>         </td>     </tr>     <tr>         <td><input type="checkbox" name="option_2"> option 2 (to determined)</td>         <td>             <select name="">                 <option value="">as:</option>                 <option value="3">1st</option>                 <option value="2">2nd</option>                 <option value="1">3rd</option>             </select>         </td>     </tr>     <tr>         <td><input type="checkbox" name="option_3"> option 3 (to determined)</td>         <td>             <select name="">                 <option value="">as:</option>                 <option value="3">1st</option>                 <option value="2">2nd</option>                 <option value="1">3rd</option>             </select>         </td>     </tr>     <tr>         <td><input type="checkbox" name="option_4"> option 4 (to determined)</td>         <td>             <select name="">                 <option value="">as:</option>                 <option value="3">1st</option>                 <option value="2">2nd</option>                 <option value="1">3rd</option>             </select>         </td>     </tr>     <tr>         <td><input type="checkbox" name="option_5"> option 5 (to determined)</td>         <td>             <select name="">                 <option value="">as:</option>                 <option value="3">1st</option>                 <option value="2">2nd</option>                 <option value="1">3rd</option>             </select>         </td>     </tr> </table> 

i need able check user did not tick more 3 options, , ratings 1st, 2nd , 3rd used once. can check javascript checks each item separately, this:

if (document.form_name.option_1.value != "") &&  (document.form_name.option_2.value != "") &&  (document.form_name.option_3.value != "") &&  (document.form_name.option_4.value != "")  {alert('you can tick 3 options'); return false}; 

but give humongous script. there easier way? may javascript or php. preferably latter, not proficient in php, need rather ready-to-use script.

thanks in advance.

you don't need php in instance (although should include server side validation php because there no guarantee validation js run on client's browser. possibly because of malicious user, or maybe because of 1 doesn't have js enabled).

you can make js shorter if add logic. iterate through each of questions. within each question, count each of checked checkboxes , when done see ones have more 3 checked.

var questions = document.getelementsbyclassname('question'), numberchecked, answers;  for(var q = 0, qlen = questions.length; q < qlen; q++) {     numberchecked = 0;     answers = questions[q].getelementsbytagname('input');      for(var = 0, alen = answers.length; < alen; a++) {         if(answers[a].type == 'checkbox' && answers[a].checked) {             numberchecked++;         }     }      if(numberchecked > 3) {         alert('you may check 3 choices question #' + (q + 1));     } }  

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -