Validating all checkboxs, plus a particular checkbox in array

Discussion in 'JavaScript' started by geowelch, Nov 3, 2006.

  1. #1
    Hi,

    I'm new to the forum and not partivularly proficient with Javascript. That said, I am trying to build a simple script which will verify whether atleast one of an array of check boxes, corresponding to vendor categories, is selected. This is for exhibitor application to an arts & crafts festival.

    Within the same script I also need to check whether a particular checkbox has been selected (the "Other" category), and if so, check that the user has typed something in the adjacent text box, specifying what the unlisted category is.


    
    function CheckCategories()
    {
    	boxes = frm_VendorAp.category.length
    	for (i = 0; i < boxes; i++) 
    	{
    		if (frm_VendorAp.category[i].checked == false)
    			{ 
    			alert("Please select a Vendor Category");
    			return false;
    			}
    
    		else if (frm_VendorAp.category[11].checked == true && 
    			frm_VendorAp.other_specified.value == "")
    			{
    			alert("Please specify your medium or product type"); frm_VendorAp.other_specified.focus(); return false;
    			}
    		else {return true;}
    	}
    
    }
    
    Code (markup):
    Below is the code for a couple of checkboxes:

    
    <input name="category" type="checkbox" value="Prints" tabindex="18">
                     
    <input name="category" type="checkbox" value="Other" tabindex="22">
    Code (markup):
    So far the script invalidates input if any of the checkboxes are unselected, though it does prompt for input if the second condition (the "Other" category) is fulfilled. The corresponding checkbox is no. 11 in the array.

    What do I need to change for the script to accept a single checkbox selection?

    Thanks
     
    geowelch, Nov 3, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Untested, but give this a try.
    
    <script type="text/javascript">
    <!--
    function CheckCategories()
    {
    	var boxes = frm_VendorAp.category;
    	var check = false;	
    	
    	for (var i in boxes) 
    	{
    		if (frm_VendorAp.category[i].checked)
    		{ 
    			check = true;
    			break;
    		}
    	}
    	
    	if (frm_VendorAp.category[11].checked && frm_VendorAp.other_specified.value == "")
    	{
    		alert("Please specify your medium or product type"); 
    		frm_VendorAp.other_specified.focus();
    		return false;
    	}
    	else if (!check)
    	{
    		alert("Please select a Vendor Category");
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    //-->
    </script>
    
    Code (markup):
     
    nico_swd, Nov 3, 2006 IP
  3. geowelch

    geowelch Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Works perfectly! Thanks nico swd!
     
    geowelch, Nov 3, 2006 IP
  4. geowelch

    geowelch Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I spoke too soon! The script works fine in Firefox, my primary browser, but in IE it displays the alert message no matter how many checkboxes are selected. In other words, it does not seem to recognize the checked property of the checked boxes at all.

    :confused:
     
    geowelch, Nov 8, 2006 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Try placing document. infront of all frm_VendorAps.

    
    
    var boxes = document.frm_VendorAp.category;
    
    ....
    
    if (document.frm_VendorAp.category[i].checked)
    
    ....
    
    etc...
    
    
    Code (javascript):
     
    nico_swd, Nov 8, 2006 IP