SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkbox validation problems

    Hey All,

    I have been working over this Javascript a few times and cannot seem to get it working correctly, I have found the javascript mostly by searching on the net and on this forum.

    What I am trying to do is to use a function with in another function to check that a group of checkboxes have atleast one checked.

    The code is as follows:

    Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
      	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
      <head>
      <title>Untitled</title>
      <script>
      // Function to check that one or more radio buttons are selected
      function is_multiple_checked() {
      
      var formObj = document['cec']['q[]'];
      var count = 0;
      	for (i=0;i<formObj.length; i++)
      	{
      		if(formObj.type == 'checkbox')
      		if(formObj[i].checked)
      		count++;
      	}
      	if (count < 1)
      	{
      	return false;
      	}
      	else {
      		
      		return true;
      	}
      }
      
      // Function to call checkbox function (?)
      function val(){
      	if (!is_multiple_checked()){
      		alert ('sel one of them');
      		return false;
      	}
      	else {
      		alert ('selected one and submitting');
      		return false;
      	}
      }
      </script>
      </head>
      
      <body>
      <form name="cec" action="/" method="post" onsubmit="return val()">
      	<input type="checkbox" name="q[]" value="1" />o 1<br />
      	<input type="checkbox" name="q[]" value="2" />o 2<br />
      	<input type="checkbox" name="q[]" value="3" />o 3<br />
      	<input type="checkbox" name="q[]" value="4" />o 4<br />
      	<input type="checkbox" name="q[]" value="5" />o 5<br />
      	<br />
      	<input type="checkbox" name="r[]" value="6" />o 6<br />
      	<input type="checkbox" name="r[]" value="7" />o 7<br />
      	<input type="checkbox" name="r[]" value="8" />o 8<br />
      	
      	<input type="submit" value="send" />
      	
      </form>
      
      
      </body>
      </html>
    I am trying to check that the q[] checkboxes are checked.

    Im not the greatest at javascript so any help would be fantastic

    Thanks in advance.
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I ask a REALLY dumb question here? If you only want the customer to be able to select ONE of the boxes why not use radio buttons instead of checkboxes????? HMMM???? Am I totally off base here or what?

  3. #3
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey dc dalton sorry man maybe I worded that incorrectly (was tired ), I need them to check ATLEAST one of the checkboxes or MORE checked.
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  4. #4
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem was in your for loop. You were not checking the individual check boxes by specifying the index. Here is an updated version that should work.
    Code:
    <html>
      <head>
      <title>Untitled</title>
      <script>
      // Function to check that one or more radio buttons are selected
      function is_multiple_checked() {
      
      var formObj = document['cec']['q[]'];
      var count = 0;
      	for (i=0;i<formObj.length; i++)
      	{
      		if(formObj[i].type == 'checkbox')
      		if(formObj[i].checked)
      		count++;
      	}
          alert(count)
      	if (count < 1)
      	{
      	return false;
      	}
      	else {
      		
      		return true;
      	}
      }
      
      // Function to call checkbox function (?)
      function val(){
      	if (!is_multiple_checked()){
      		alert ('sel one of them');
      		return false;
      	}
      	else {
      		alert ('selected one and submitting');
      		return true;
      	}
      }
      </script>
      </head>
      
      <body>
      <form name="cec" action="page1.html" method="post" onsubmit="return val()">
      	<input type="checkbox" name="q[]" value="1" />o 1<br />
      	<input type="checkbox" name="q[]" value="2" />o 2<br />
      	<input type="checkbox" name="q[]" value="3" />o 3<br />
      	<input type="checkbox" name="q[]" value="4" />o 4<br />
      	<input type="checkbox" name="q[]" value="5" />o 5<br />
      	<br />
      	<input type="checkbox" name="r[]" value="6" />o 6<br />
      	<input type="checkbox" name="r[]" value="7" />o 7<br />
      	<input type="checkbox" name="r[]" value="8" />o 8<br />
      	
      	<input type="submit" value="send" />
      	
      </form>
      
      
      </body>
      </html>

  5. #5
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fantastic thanks very much requestcode, that does exactly what it is meant to, happy new years!
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.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
  •