Solved

validate checkbox

Posted on 2013-01-07
5
198 Views
Last Modified: 2013-01-08
Experts-

Can someone help me with ensuring that at least one check box is selected?

<td width="110"><input type="checkbox" class="checkbox" name="windows" value="Windows" /><label for="windows">Windows</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="gutterhelmet" value="Gutter Helmet" /><label for="gutterhelmet">Gutter Helmet</label></td>
</tr>
<tr>
<td width="110"><input type="checkbox" class="checkbox" name="roofing" value="Roofing" /><label for="roofing">Roofing</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="icedefense" value="Ice Defense" /><label for="icedefense">Ice Defense</label></td>
</tr>
<tr>
<td width="110"><input type="checkbox" class="checkbox" name="siding" value="Siding" /><label for="siding">Siding</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="drenergy" value="Dr. Energy Saver" /><label for="drenergy">Dr. Energy Saver</label></td>

Any help would be appreaicted

Thanks
Nick
0
Comment
Question by:nmarano
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Big Monty
ID: 38752165
here's a good approach if you have the option to use jQuery and the validator plug-in:

http://stackoverflow.com/questions/11512448/validation-for-at-least-one-checkbox
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 38753595
<script>
var count=0;
function checkBoxValid()
{
		input= document.getElementsByTagName("input");

		for(i=0;i<input.length;i++)
		{
			if(input[i].type =='checkbox' && input[i].checked)
			{
			 count++;
			}

		}

		if(count == 0)
		{
		  alert("please select atleast once check box")
		  return false;
		}

}
</script>

<form id='frm' onsubmit=' return checkBoxValid()'>
<td width="110"><input type="checkbox" class="checkbox" name="windows" value="Windows" /><label for="windows">Windows</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="gutterhelmet" value="Gutter Helmet" /><label for="gutterhelmet">Gutter Helmet</label></td>
</tr>
<tr>
<td width="110"><input type="checkbox" class="checkbox" name="roofing" value="Roofing" /><label for="roofing">Roofing</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="icedefense" value="Ice Defense" /><label for="icedefense">Ice Defense</label></td>
</tr>
<tr>
<td width="110"><input type="checkbox" class="checkbox" name="siding" value="Siding" /><label for="siding">Siding</label></td>
<td width="130"><input type="checkbox" class="checkbox" name="drenergy" value="Dr. Energy Saver" /><label for="drenergy">Dr. Energy Saver</label></td>
<td width="130"><input type="submit" class="checkbox" name="submit" value="submit" /></td>
</form>

Open in new window

0
 
LVL 2

Author Comment

by:nmarano
ID: 38754770
Big- Thanks, but I am unable to use jQuery.

chaituu-  Thank you!  The code works perfectly.  One last question, is there a way for me to remove where the pop up says, "The page at ....." and only have it say Please select at least one check box?

THanks
Nick
image002.png
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38755233
actuallly am not getting that pop up ;may be you need to chnage some settings in the browser.i have tested in IE8 and not getting any popup message like that.
0
 
LVL 2

Author Closing Comment

by:nmarano
ID: 38755433
Thank you very much for your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now