Solved

At least one checkbox selected.

Posted on 2013-01-09
6
439 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
Suggested Courses

624 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