Solved

At least one checkbox selected.

Posted on 2013-01-09
6
435 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

831 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