javascript - Loop through and validate multiple radio inputs -


js:

function validateform() {     var radios = document.getelementsbyname("dquestion[1]");     var formvalid = false;      var = 0;     while (!formvalid && < radios.length) {         if (radios[i].checked) formvalid = true;         i++;             }      if (!formvalid) alert("must check 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 make inputs checked, , if not, can't submit form. have 10 radio inputs each question, , form has 25 questions, use dquestion['number'] each name. using code test dquestion[2] can't checked. why? how going loop 25 questions?

i tried code, didn't work:

var dnames = ['dquestion[1]','dquestion[2]']  function validateform() {      var radios = [];     var formvalid = false;      (var = 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 option!");     return formvalid; } 

i wrap each set of radio buttons pertaining particular question around div , check each div see if @ least 1 radio button 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) {         // <input> tags...         var inputs = sel.getelementsbytagname('input');         (var k=0; k<inputs.length; k++) {             // if have more 1 radio group, check name attribute             // 1 want in && chx[i].name == 'choose'             // return true function on first match of checked item             if( inputs[k].checked )                 return true;         }         // end of loop, return false         return false;     } 

jsfiddle:

here working jsfiddle reference.

credit:

isoneinputchecked() function taken michael berkowski's reply in different thread (link).


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -