SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict frankiehots's Avatar
    Join Date
    Jan 2002
    Location
    U.S.A. *Blue State*
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with validating checkbox array

    I am trying to use JavaScript to require that at least one one checkbox of multiple checkbox choices is selected. My validation works except that I need to add a "[]" to the checkbox name for separate server processing. Once I do that, my validation no longer works.

    Here is my code:
    Code:
      <script type="text/javascript">
      function formValidate(){
         with(document.checkboxform){
      			  checkSelected = false;
      			  for (i=0;  i < infocheckbox[].length;  i++)	{
      				   if (infocheckbox[][i].checked){
      				   checkSchool = true;
      			   }
      		 if (!checkSelected){
      				alert('Please choose at least one checkbox');
      				return false;
      			} 
      		else {
      			return true;
      			}
      
        } 	 
      }
    </script>
    When I use infocheckbox[] the validation does not work, but if I remove the [] and just use infocheckbox, it works just fine. Any idea on what I need to do to be able to use []?

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My validation works except that I need to add a "[]" to the checkbox name for separate server processing.
    Can you expand on that a bit? What does your server processing need to do that requires the [] be present in the javascript?

  3. #3
    SitePoint Addict frankiehots's Avatar
    Join Date
    Jan 2002
    Location
    U.S.A. *Blue State*
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel
    Can you expand on that a bit? What does your server processing need to do that requires the [] be present in the javascript?
    Sorry for not providing enough info. I am using PHP to detect for the presence of an array and the [] is typically how I've done it in HTML forms. For example:

    HTML Code:
     <input type="checkbox" name="infocheckbox[]" value="red">Red
     <input type="checkbox" name="infocheckbox[]" value="blue">Blue
     <input type="checkbox" name="infocheckbox[]" value="green">Green
     <input type="checkbox" name="infocheckbox[]" value="purple">Purple
    By using the "[]", PHP sees an array (is_array) and then can handle it differently than other form POST variables.

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    have you tried this:

    Code:
      			  for (i=0;  i < infocheckbox.length;  i++)	{
      				   if (infocheckbox[i].checked)
    ?

  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript doesn't automatically assemble an array from the elements using the PHP name convention you mention. You'll have to create your own array with:

    document.getElementsByName('infobox[]')

    Assign the output to a variable and it will provide you a 0-based array with which to access the elements.

  6. #6
    SitePoint Addict frankiehots's Avatar
    Join Date
    Jan 2002
    Location
    U.S.A. *Blue State*
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JVLB
    JavaScript doesn't automatically assemble an array from the elements using the PHP name convention you mention. You'll have to create your own array with:

    document.getElementsByName('infobox[]')

    Assign the output to a variable and it will provide you a 0-based array with which to access the elements.
    Perfect solution JVLB, thanks a lot. That will come in handy for the future too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •