limiting number of checkboxes

Discussion in 'OT Technology' started by babygodzilla, Jul 24, 2003.

  1. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    im using php and i have a form consisting of checkboxes. what is the best way of limiting the number of checkboxes a user can check? the problem is i never know how many checkboxes there will be. it could be 1 it could be 100. and since this is a search engine, the names of the checkboxes depend on the results. what i have right now is a javascript function that is called when a box is checked. it increments a variable and when it reaches 6 an alert will pop. im not sure why its not working.

    Code:
    function checkCount() {
    
    	var NewCount = 0
    	
    	NewCount = NewCount + 1
    
    	if (NewCount == 6)
    	{
    		alert('Pick 5 classes only')
    		document.checks; 
    		return false;
    	}
    }
    i actually got part of this code from a tutorial and it's supposed to work. but i'm thinkin that everytime this function is called, doesn't it set NewCount back to zero?

    thanks!!!
     
  2. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    By no means am I a javascript person but I am willing to bet you problem is with the fact that NewCount is declared in the function and initialized to 0, then incremented by 1. So, every time it does the if check, NewCount is always 1. What you will need to do is declare the NewCount variable outside of the function so it doesn't always get reset.
     
  3. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Check out http://www.irt.og[/url and their Ja... as keeping track of which ones got checked).
     

Share This Page