Jump to content

Check Button validation help


psychowolvesbane

Recommended Posts

I got 7 checkboxes which are basically sizes for clothing, XS, S, M, L, XL, XXL, and 3XL and I am trying to validate them using javascript, all I need is for it to know if any have been checked, and if none haven't bring up the warning message I already have in place. The only problem I've been getting so far is accessing the check button group I've made, which is an array called Sizes[].

 

Here's the form code:

<?php
$CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL");
?>
<span id="mySpan7" class="errmsg"><?php echo $Message_ASizes ?></span><br>
<span class="head4">Available Sizes:</span><br>
<table id="Sizes"><tr>

<?php
$X=0;
       
while($X!=7)
{
   $Size = $CBSizes[$X];
   echo"<td>$Size:</td>";
   echo"<td><input type='checkbox' name='Sizes[]' value='$Size'"; 
   if($SizeA[$Size]==true){echo"Checked";}?>></td></tr><tr>
   <?php
   $X++;
}
?>
</tr></table>

 

As you can see I use the array CBSizes to grab the names per value of $X, and place all the Sizes into the Sizes[] array. I have a method already in PHP for validating it but I haven't got a working one for Javascript.

 

Here's the code that's in my main validation function:

 

   numBoxesSelected = 0;
   var valuesSelected = new Array;

   for (var i=1; i <= numBoxes; i++) 
   {
      if (eval("document.AddProduct.Sizes" + i + ".checked")) 
      {
         valuesSelected[numBoxesSelected] = eval("document.AddProduct.Sizes" + i + ".value");
 numBoxesSelected++;
      }
   }
   if(numBoxesSelected ==0)
   {
      document.getElementById('mySpan7').innerHTML='! Please choose any relevant Sizes!';
   }
   else
   {
      document.getElementById('mySpan7').innerHTML='';
   }

 

Thanks in advance for those that help.

Link to comment
Share on other sites

 

I personally would do this; I would give each input box a separate id and access them individually when you validate with JS.

 

Set a variable in your PHP script; something like:

 

<?php
$CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL");
?>
<span id="mySpan7" class="errmsg"><?php echo $Message_ASizes ?></span><br>
<span class="head4">Available Sizes:</span><br>
<table id="Sizes"><tr>

<?php
$X=0;

$i="0";
       
while($X!=7)
{
$i = $i + 1;
   $Size = $CBSizes[$X];
   echo"<td>$Size:</td>";
   echo"<td><input type='checkbox' id='field$i' name='Sizes[]' value='$Size'"; 
   if($SizeA[$Size]==true){echo"Checked";}?>></td></tr><tr>
   <?php
   $X++;
}
?>
</tr></table>

 

Then validate your checkboxes that way.

Link to comment
Share on other sites

BTW I have got this solved via another source, the code I used without using ID's was:

 

   var valuesSelected=[];
   var checks=document['AddProduct']['Sizes[]'], i=0, t='', c;
   while(c=checks[i++])
   {
      c.checked?valuesSelected[valuesSelected.length]=c.value:null;
   }
   !valuesSelected.length?t='! Please choose any relevant Sizes!':null;
   document.getElementById('mySpan7').innerHTML=t;

 

Topic Solved!

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.