SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check checkbox array

    I have a checkbox array and would like to have the submit button check to make sure that at least 1 of the boxes is checked.

    Here's my example checkbox code for use with php:
    Code:
    <input type="checkbox" name="field[certificate]" value="$applicant[apid]">
    <input type="checkbox" name="field[interview]" value="$applicant[apid]">
    How would I go about creating such a thing?

    Thanks
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this perhaps:
    HTML Code:
    [size=1]<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    
    	<head>
    		<title>Make sure at least one check-box is selected.</title>
    	</head>
    
    	<script language="JavaScript" type="text/javascript"><!-- 
    
    		function AtLeastOneChecked ( sListOfCheckIDs ) {
    
    			if ( ! document.getElementById ) return true;
    
    			sListOfCheckIDs = sListOfCheckIDs.split ( ',' );
    
    			var bAtLeastOneIsChecked = false;
    
    			for ( var i = 0 ; i < sListOfCheckIDs.length ; i ++ ) {
    				if ( document.getElementById ( sListOfCheckIDs [ i ] ).checked ) {
    					bAtLeastOneIsChecked = true;
    					break;
    				}
    			}
    
    			if ( ! bAtLeastOneIsChecked ) alert ( 'Dude! You like, *totally* need to have at least one those check-boxes selected!' );
    
    			return bAtLeastOneIsChecked;
    
    		}
    
    	// --></script>
    
    	<body>
    
    		<form method="post" onsubmit="return AtLeastOneChecked ( 'chk1,chk2,chk3,chk4,chk5' );">
    
    			<ul style="list-style: none; margin: 0; padding: 0 0 15px 0;">
    				<li><input id="chk1" type="checkbox" name="chk" value="1" /> <label for="chk1">Option #1</label></li>
    				<li><input id="chk2" type="checkbox" name="chk" value="2" /> <label for="chk2">Option #2</label></li>
    				<li><input id="chk3" type="checkbox" name="chk" value="3" /> <label for="chk3">Option #3</label></li>
    				<li><input id="chk4" type="checkbox" name="chk" value="4" /> <label for="chk4">Option #4</label></li>
    				<li><input id="chk5" type="checkbox" name="chk" value="5" /> <label for="chk5">Option #5</label></li>
    			</ul>
    
    			<input type="submit" />
    
    		</form>
    
    	</body>
    
    </html>[/size]
    Of course, that's just my opinion. I could be wrong.

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need to validate any other fields in the form, or just this checkbox?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just need to see if at least one checkbox is checked.

    Thanks
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


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
  •