Solved

client validation on click of checkbox in a nested repeater

Posted on 2008-10-30
6
3,311 Views
Last Modified: 2013-11-11
I have a repeater which shows current month records from database based upon month and year selection from dropdownlist at the top of page. This repeater has 3 textboxes per row  (date of month) and a nested repeater. Nested repeater contains checkbox and is populated from database. I want to make sure using javascript and custom validators that at least one checkbox in each row should be selected.
0
Comment
Question by:Munawar Hussain
[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
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22843548
Hello,
Looking from your description, you seem to have
for each outer repeater item,
  you hav 3 textboxes  and 1 child repeater  (which further down contains 1 checkbox).

But this means you only have 1 checkbox per item.
And you want to ensure all of these are selected?
I guess I'm missing something out here.
0
 
LVL 12

Author Comment

by:Munawar Hussain
ID: 22847693
In child repeater I have list of checkboxes and one of those checkboxes should be checked.
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22851252
Hello,
Got it, I have created a sample javascript for you, just paste it as a .html file and run locally and try pressing the submit button without selecting checkboxes in each row, it will give error and tell you that you have to select these many checkboxes atleast i.e. once per each row.

This is html and javascript , but looking at the code and html I have produced, you can easily port it in your repeater, just make sure you keep ids properly and change variables accordingly in this script.

Hope it solved your problem.

Happy Javascripting!

<html>
<body>
 
<form id="form1" >
<div id="_main" style="width:100px;">
 
<div id="div1" style="background-color:Gray;">
	<input type="CHECKBOX" id="chk_1" value="This...">
	<input type="CHECKBOX" id="chk_2" value="This...">
	<input type="CHECKBOX" id="chk_3" value="This...">
</div>
Group 2
 
<div id="div2" style="background-color:Gray;">
	<input type="CHECKBOX" id="chk_1" value="This...">
	<input type="CHECKBOX" id="chk_2" value="This...">
	<input type="CHECKBOX" id="chk_3" value="This...">
</div>
 
 
<input type="hidden" id="hdn1" value="div1,div2">
</div>
 
<input type="submit" value="submit" onclick="checkform()" >
</form>
</body>
</html>
 
<script type="text/javascript">
 
function checkform()
{
  var v_main  = document.getElementById("_main");
  var v_sub = v_main.getElementsByTagName('div');
  
  var i=0;
  var faultCounter=0;
  while(i<v_sub.length)
  {	
	var v_sub2 = v_sub[i].getElementsByTagName('input');
	var innerChecked=0;
        j=0;
	while(j<v_sub2.length)
	{
		if(v_sub2[j].checked==true)
		{
			innerChecked=1;
		}	
		j++;
	}
	
	if(innerChecked==0) //i.e. no checkbox is checked
	{
	  faultCounter = faultCounter + 1;
	}
	i++;
  }
 
  if (faultCounter>0)
  {
    alert("Atleast select one checkbox frome each row, " + faultCounter + " checkbox needs to be selected yet.");
  }
  else
  {
	alert("All O.K.");
  }
}
 
 
</script>

Open in new window

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 12

Expert Comment

by:techExtreme
ID: 22851307
Also forgot to mention, after you get familiar with the script I posted above,
you will notice tht
there should be one single outer div element,

then elements like div1, div2 ... can probably be once per itemtemplate of repeater,
i.e. if you have two dataitems, you should render div1,div2...  , if you have three dataitems, your repeater shoudl produce div1,div2,div3 like that.

Now inside of these divs (div1, div2... ) you can have your checkboxes.
ids of checkboxes doesn't matter as of now.


You can use this logic with two repeaters, or 1 repeater,
its just how you arrange the divs and how they render dynamically!
0
 
LVL 12

Author Comment

by:Munawar Hussain
ID: 22851970
Hi techExtreme,

    Thanks for your time. I pasted above code in html file but when I click one checkbox from one row and dont check any from other row It prompts me with alert and then the checkbox I checked also looses selection. Why so ?

Regards,
Asif Hameed
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22852551
Its just html buddy. Once you prepare it with dynamic code and in aspx page , the session will be maintained there.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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…
Suggested Courses

626 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