Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp: problem with logic in function

Status
Not open for further replies.

selaine

Programmer
Oct 11, 2001
85
US
I need to evaluate the following checkbox fields to make sure the user has selected at least one of the checkboxes. I fugure it's got to be a problem with my logic because if no boxes are checked, a few boxes are checked or all of the boxes are checked, the function shows the alert and returns false every time!

function DoValidation()
{
var frm = document.forms["supplyreg"];
if ((!(frm.ck_Automotive.checked)) || (!(frm.ck_BldgMat.checked)) || (!(frm.ck_Chemicals.checked)) || (!(frm.ck_Computing.checked)) || (!(frm.ck_ConstrucContr.checked)) || (!(frm.ck_FloorCvrng.checked)))
{
alert('Click a box...');
return false;
}
else
{
return true;
}
}

Can anyone see a problem with this logic? Thanks!!!
[nosmiley]
 
try this:
Code:
function DoValidation()
{
     var frm = document.forms["supplyreg"];
     var blnChecked = false;

     while ((blnChecked ==false) && (i < frm.elements.length))
     {
          if ((frm.elements[i].type = &quot;checkbox&quot;) &&(frm.elements[i].checked == true)) {blnChecked = true;}
           i++;
      }
      if (blnChecked == false)
      {
           alert(&quot;Please check at least one box...&quot;);
           return false;
      }
      else {return true;}
}

Hope this helps :)

Take Care,
Mike
 
I forgot to mention that there are multiple checkboxes, but there is one particular checkbox in the form that is optional (thats why I didn't do the loop type process). The user can choose to be notified by email or not (ck_email), but must choose from at least one of the remaining checkboxes. With the code you presented, how would I do it in a loop (would be much more efficient), but exclude the ck_email checkbox from the conditions?
 
add a custom tag to the checkbox called 'option' and set it to true:

<checkbox ooptional=&quot;true&quot;>

then update the loop like so:
Code:
try this:

function DoValidation()
{
     var frm = document.forms[&quot;supplyreg&quot;];
     var blnChecked = false;

     while ((blnChecked ==false) && (i < frm.elements.length))
     {
          if (frm.elements[i].type == &quot;checkbox&quot;)
               if ((frm.elements[i].checked == true) && (frm.elements[i].optional != &quot;true&quot;))
{blnChecked = true;}
           i++;
      }
      if (blnChecked == false)
      {
           alert(&quot;Please check at least one box...&quot;);
           return false;
      }
      else {return true;}
}

Take Care,
Mike
 
I discovered the problem with my logic was (duh!!) using OR logic instead and AND. By using OR I was telling it that all of the boxes weren't checked to show the alert; by changing logic to and it told the function if none of the checkboxes were checked. Brain aneurysm!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top