Solved

At least one checkbox selected.

Posted on 2013-01-09
6
437 Views
Last Modified: 2013-01-09
Please take a look at the below functions.

Part of the validation script calls a function to test for checked checkboxes. These checkboxes are themselves dynamic and all is well so long as there are more than one checkbox. This is normally the case, however, on rare occasions there can be just one.

If this happens it always displayes the alert message even when that one checkbox has been selected.

And advice.

Kind regards

A
-----------------------------------------------
function isChecked(chks)
{
      var rval = false;
      for(var i=0; i<chks.length; i++)
      {
            if(chks[i].checked)
            {
                  rval = true;
                  break;
            }
      }
      return rval;
}

<!--  Part of the validation script -------------------

if(document.getElementById("selAccType").value == 1)
{
         if (!isChecked(document.form_Dyno["chk_MyStuID"]))
        {
       alert("Please select at least one option!");
       return false;
        }
}

Open in new window

0
Comment
Question by:Abiel de Groot
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38758620
please post the html .
0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 38758638
The button calls the validation script. "checkFormEntry()"

<input name="Submit" type="submit" class="ButGeneral" value="<%=PlaceText(185)%>" onclick="checkFormEntry()"/>

If all is well it submits the form. Part of the validation script checks the checkboxes.

if(document.getElementById("selAccType").value == 1)
{
         if (!isChecked(document.form_Dyno["chk_MyStuID"]))
        {
       alert("Please select at least one option!");
       return false;
        }
}
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38758673
remove onclick function in input type submit and put that in form onSubmit.

<form onSubmit='return checkFormEntry()'>

<input name="Submit" type="submit" class="ButGeneral" value="<%=PlaceText(185)%>" />
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
Ironhoofs earned 500 total points
ID: 38758999
If there is only one checkbox, document.form_Dyno["chk_MyStuID"] doesnt return an array of objects and chks.length will be undefined. A small change to isChecked() will fix this:

function isChecked(chks) {
      var rval = false;
      if (!chks.length) {
         rval = chks.checked;
      }
      else {
         for(var i=0; i<chks.length; i++)  {
            if(chks[i].checked)
            {
                  rval = true;
                  break;
            }
         }
      }
      return rval;
}

Open in new window

0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38759159
<script>
function isChecked(chks) {
      var rval = false;
	  alert(chks.length)
   
         for(var i=0; i<chks.length; i++)  {
            if(chks[i].checked)
            {
                  rval = true;
                  break;
            }
         }
      
	  
	  if(!rval)
	  {
	  alert("plz select one checkbox")
	  }
      return rval;
} 

</script>

<form>
<input type='checkbox' name='check' value='1'>
<input type='checkbox' name='check' value='2'>
<input type='checkbox' name='check' value='31'>
<input type='button' name=button value='submit' onclick='isChecked(document.getElementsByName("check") )'>
</form>

Open in new window

0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
ID: 38759176
That did it.

Many thanks.

A.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to filter by key press ? 6 54
Use javascript to open specific place on an accordian in other page 8 39
Validating Date 4 28
Table doesn't show the lines! 3 23
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question