Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

At least one checkbox selected.

Posted on 2013-01-09
6
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Accepted Solution

by:
Ironhoofs earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

670 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