jquery - validate a drop down list using javascript? -
<form name="form1" method="post" action="survey.php"> <p>q1: how rate ahmed ebaid? <p> <input type='radio' name='q1' value='1' id='q1'>1 <p> <input type='radio' name='q1' value='2' id='q1'>2 <p> <input type='radio' name='q1' value='3' id='q1'>3 <p> <input type='radio' name='q1' value='4' id='q1'>4 <p> <input type='radio' name='q1' value='5' id='q1'>5<span style='color:red' id='radio_error'></span><pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>', 1'</font> <i>(length=3)</i> </pre> <p>q2: best best friend?</br> <select name='q21' id='q2'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q22' id='q2'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q23' id='q2'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <p>q3: how rate abdelhai ebaid? <p> <input type='radio' name='q3' value='1' id='q3'>1 <p> <input type='radio' name='q3' value='2' id='q3'>2 <p> <input type='radio' name='q3' value='3' id='q3'>3 <p> <input type='radio' name='q3' value='4' id='q3'>4 <p> <input type='radio' name='q3' value='5' id='q3'>5<span style='color:red' id='radio_error'></span> <p>q4: best best friend?</br> <select name='q41' id='q4'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q42' id='q4'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q43' id='q4'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q44' id='q4'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <p>q5: how rate attitude of farida ebaid? <p> <input type='radio' name='q5' value='1' id='q5'>1 <p> <input type='radio' name='q5' value='2' id='q5'>2 <p> <input type='radio' name='q5' value='3' id='q5'>3 <p> <p><span style='color:red' id='radio_error'></span> <p> <input type='hidden' name='h2' value=', 1'> <p> <input type="submit" name="submit1" value="click here vote">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script> $(function(){ if ($('form').length > 0) { $('form').submit(function(e){ var answers = ''; var len = 5; $('input[type=radio]:checked').each(function() { if (answers !== '') { answers += ','; } answers += $(this).val(); //alert(answers); }) $('input[name=h2]').val(answers); (var = 1; <= len; i++ ) { var qnum = 'q'+i; //document.write(qnum); if ($('#'+qnum+'').is(':radio')) { if (($('input[name='+qnum+']:checked').length == 0) ){ alert("no selection made "+ qnum); return false; } } else if ($('#'+qnum+'').is('select')) { var j = 1; var nominatefriend = qnum+j; alert (nominatefriend); var check = ''; var chosen = new array(); while (check = document.getelementsbyname("nominatefriend")) { alert(check); var len = document.form1[nominatefriend].length; alert (len); (var = 0; < len; i++) { if (document.form1[nominatefriend[i]].selected) { chosen[j] = document.form1.nominatefriend[i].value; alert (chosen[j]); } } j++; nominatefriend = qnum+j; alert (nominatefriend); } } } // loop }); // form submit function }// form.length })//function
when trying execute above code, following error: typeerror: document.form1[nominatefriend[i]].selected. i'm using nominatefriend[i] iterate through options, outputs nothing me.
you declare variable this, it's number (integer)
var nominatefriend = qnum+j;
yet access array:
document.form1[nominatefriend[i]].selected
for two-dimensional array, you'd need
document.form1[nominatefriend][i].selected
however, pay attention comments, 1 duplicate ids in document.
Comments
Post a Comment