View Single Post
  #1 (permalink)  
Old Apr 23rd, 2008, 17:10
psycho wolvesbane psycho wolvesbane is offline
Junior Member
Join Date: Feb 2008
Location: England
Age: 21
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Question [SOLVED] Validation code problem

I have a piece of code that validates whether one button has been selected from a bunch of dynamically created radio buttons.

Code: Select all
//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.
Reply With Quote