psychowolvesbane Posted April 23, 2008 Share Posted April 23, 2008 I have a piece of code that validates whether one button has been selected from a bunch of dynamically created radio buttons. //Validation Code var checked = false; var Sizes = document.BuyProduct.Sizes; for (var i=0; i<Sizes.length; i++) { if (Sizes[i].checked) { checked = true; } } if(!checked) { Errors++ document.getElementById('mySpan1a').style.display='block'; document.getElementById('mySpan1a').innerHTML='Please Select a Size!'; document.getElementById('mySpan1b').style.display='inline'; document.getElementById('mySpan1b').innerHTML='! '; } else { document.getElementById('mySpan1a').style.display='none'; document.getElementById('mySpan1a').innerHTML=''; document.getElementById('mySpan1b').style.display='none'; document.getElementById('mySpan1b').innerHTML=''; } // Form code <?php echo $Err1?><span class="mySpan1b" id="mySpan1b"></span><span class="head4">Available Sizes:</span><span class="errmsg"> *</span> <table style="position:relative;"><tr> <?php $counter=0; $X=0; $i=0; $IsEmpty1 == false; while ($IsEmpty1 == false) { if($AvailableSizes[$X] != "") { $id1 = "Size".$i; if($counter%6==0&&$counter!=0) echo "</tr><tr>"; echo"<td>$AvailableSizes[$X]</td>"; echo "<td><label><input type='radio' id='$id1' name='Sizes' value='$AvailableSizes[$X]'"; if($AvailableSizes[$X] == $SizeSel){echo "Checked";} echo"></label></td></tr><tr>"; ++$counter; } else { $IsEmpty1 = true; } $i++; $X++; } ?> </tr></table> The validation code works, but to a point. This code assumes that there will be more than 1 radio button to choose from and becomes an array, however occassionally there will be only a single button which doesn't turn the selection into an array, and thus stops the validation code from working and displaying the error message for that option. So what I need is a modified version of the validation that can account for when the selection is an array and when it is only a single option as well. Quote Link to comment Share on other sites More sharing options...
nogray Posted April 23, 2008 Share Posted April 23, 2008 You can try a small if statment to check if Sizes is an array if (Sizes.length){ for loop } else { single element } Quote Link to comment Share on other sites More sharing options...
psychowolvesbane Posted April 23, 2008 Author Share Posted April 23, 2008 Unfortunately Sizes.length is only defined with the multi selection, it says it's undefined otherwise. Quote Link to comment Share on other sites More sharing options...
nogray Posted April 23, 2008 Share Posted April 23, 2008 that's why the if statment is there if you getting an error with if statment try to change it to if ((typeof Sizes.length) != "undefined"){ ..... Quote Link to comment Share on other sites More sharing options...
psychowolvesbane Posted April 23, 2008 Author Share Posted April 23, 2008 Ah got it sorted, here is the code based on you're first post: var checked = false; var Sizes = document.BuyProduct.Sizes; if (Sizes.length){ for (var i=0; i<Sizes.length; i++) { if (Sizes[i].checked) { checked = true; } } } else if(Sizes.checked) { checked=true; } if(!checked) { Errors++ document.getElementById('mySpan1a').style.display='block'; document.getElementById('mySpan1a').innerHTML='Please Select a Size!'; document.getElementById('mySpan1b').style.display='inline'; document.getElementById('mySpan1b').innerHTML='! '; } else { document.getElementById('mySpan1a').style.display='none'; document.getElementById('mySpan1a').innerHTML=''; document.getElementById('mySpan1b').style.display='none'; document.getElementById('mySpan1b').innerHTML=''; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.