Solved

At least one checkbox selected.

Posted on 2013-01-09
6
423 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now