Jump to content

[SOLVED] Validation code problem


psychowolvesbane

Recommended Posts

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.

 

Link to comment
Share on other sites

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='';
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.