Connecting Tech Pros Worldwide Forums | Help | Site Map

Checkboxlist Validation

sheenattt's Avatar
Newbie
 
Join Date: Nov 2006
Posts: 20
#1: Nov 10 '06
I am using checkboxlist in asp.net.I cant get an alert with JavaScripting when the checkboxlist is not selected.For checkbox the command is checkbox.checked,can anyone tell me what is the command for checkboxlist?

Needs Regular Fix
 
Join Date: Jul 2006
Posts: 269
#2: Nov 10 '06

re: Checkboxlist Validation


<!-- This script is available free online at MediaZeal Web Design --->
<!-- http://mediazeal.com --->


<script language="JavaScript" type="text/JavaScript">
<!--//hide script
function checkme() {
missinginfo = "";
if (!document.form.agree.checked) {
missinginfo += "\n - You must agree to the terms";
}
if (missinginfo != "") {
missinginfo ="__________________________________\n" +
"Required information is missing: \n" +
missinginfo + "\n__________________________________" +
"\nPlease complete and resubmit.";
alert(missinginfo);
return false;
}
else {
return true;
}
}

// --->
</script>



Paste this code into the body section
<form name="form" method="post" action="#" onSubmit="return checkme();">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Name: </td>
<td> <input type="text" size="20" name="name"></td>
</tr>
<tr>
<td>Email: </td>
<td> <input type="text" size="20" name="email"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="checkbox" name="agree" value="agree_terms"> I agree to the terms</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Clear"></td>
</tr>
</table>
</form>

Quote:
<!--

This script is available free online at MediaZeal Web Design --->
Familiar Sight
 
Join Date: Oct 2006
Posts: 141
#3: Nov 10 '06

re: Checkboxlist Validation


hi,

I think this solves ur problem

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head runat="server">
  4.     <title>Untitled Page</title>
  5.     <script type="text/javascript">
  6.  function  fuc()
  7.  {
  8.  
  9. var chkList1= document.getElementById ("names");
  10. var arrayOfCheckBoxes= chkList1.getElementsByTagName("input");
  11. for(var i=0;i<arrayOfCheckBoxes.length;i++)
  12. {
  13. alert(arrayOfCheckBoxes[i].checked);
  14. }
  15. }
  16.  
  17.  
  18.  </script>
  19.  
  20. </head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.  
  24.  <asp:CheckBoxList ID="names" runat="server">
  25.  
  26.      <asp:ListItem Value="1" Text="1"  ></asp:ListItem>
  27.      <asp:ListItem Value="2" Text="2" ></asp:ListItem>
  28.      <asp:ListItem Value="3" Text="3" ></asp:ListItem>
  29.      <asp:ListItem Value="4" Text="4" ></asp:ListItem>
  30.      <asp:ListItem Value="5" Text="5" ></asp:ListItem>
  31.  
  32.      </asp:CheckBoxList>
  33.  
  34.  
  35.  
  36.     </form>
  37. </body>
  38. </html>
  39.  
  40.  















Quote:

Originally Posted by sheenattt

I am using checkboxlist in asp.net.I cant get an alert with JavaScripting when the checkboxlist is not selected.For checkbox the command is checkbox.checked,can anyone tell me what is the command for checkboxlist?

sheenattt's Avatar
Newbie
 
Join Date: Nov 2006
Posts: 20
#4: Nov 20 '06

re: Checkboxlist Validation


Thank you for your help but this is not working for Checkboxlist.If you can help plz tell me what shld I use for checkboxlist.
Familiar Sight
 
Join Date: Oct 2006
Posts: 141
#5: Nov 20 '06

re: Checkboxlist Validation


hi,

once post ur code so that i can understand because the one i posted is for the checkboxlist only










Quote:

Originally Posted by sheenattt

Thank you for your help but this is not working for Checkboxlist.If you can help plz tell me what shld I use for checkboxlist.

sheenattt's Avatar
Newbie
 
Join Date: Nov 2006
Posts: 20
#6: Nov 21 '06

re: Checkboxlist Validation


Thanku so much for your help here am pasting my code,do help me.Thanx

Expand|Select|Wrap|Line Numbers
  1. void Page_Load()
  2.     {
  3.         Button1.Attributes.Add("onClick", "return checkbox_checker()");
  4.     }
  5.  
  6.     protected void Button1_Click1(object sender, EventArgs e)
  7.     {
  8.         //further action
  9.     }
  10. </script>
  11. <html>
  12. <head>
  13. <SCRIPT LANGUAGE="JavaScript">
  14.  
  15. function checkbox_checker()
  16. {
  17. for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
  18. {
  19. if (checkbox_form.checkbox[counter].checked)
  20. {
  21.  return true;
  22. }
  23. else
  24. {
  25. alert("Not checked");
  26. return false;
  27. }
  28. }
  29.  
  30. }
  31. </script>
  32.  
  33.  
  34. </head>
  35. <body>
  36.     <form id=checkbox_form runat="server">
  37.         <p style="text-align: left">  
  38.             <asp:CheckBoxList ID="checkbox1" runat="server">
  39.                 <asp:ListItem>sheena</asp:ListItem>
  40.                 <asp:ListItem>sheenat</asp:ListItem>
  41.                 <asp:ListItem>sheenatt</asp:ListItem>                
  42.             </asp:CheckBoxList>&nbsp;</p>
  43.         <p style="text-align: left">
  44.             <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
  45.         </p>      
  46.  
  47.     </form>
  48. </body>
  49. </html>
  50.  
Familiar Sight
 
Join Date: Oct 2006
Posts: 141
#7: Nov 23 '06

re: Checkboxlist Validation


hi,
write like this
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="checkbox_checker()" />

place this script



<script type="text/javascript">

function checkbox_checker1()
{

var flag=0;
var chkList1= document.getElementById ("checkbox1");
var arrayOfCheckBoxes= chkList1.getElementsByTagName("input");
for(counter = 0; counter<arrayOfCheckBoxes.length; counter++)
{
if (arrayOfCheckBoxes[counter].checked)
{
flag=1;
}
else
{
alert("not checked");
return false;
}
}
if(flag==1)
{
alert('checked');
return true;
}
}
</script>




Quote:

Originally Posted by sheenattt

Thanku so much for your help here am pasting my code,do help me.Thanx

Expand|Select|Wrap|Line Numbers
  1. void Page_Load()
  2.     {
  3.         Button1.Attributes.Add("onClick", "return checkbox_checker()");
  4.     }
  5.  
  6.     protected void Button1_Click1(object sender, EventArgs e)
  7.     {
  8.         //further action
  9.     }
  10. </script>
  11. <html>
  12. <head>
  13. <SCRIPT LANGUAGE="JavaScript">
  14.  
  15. function checkbox_checker()
  16. {
  17. for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
  18. {
  19. if (checkbox_form.checkbox[counter].checked)
  20. {
  21.  return true;
  22. }
  23. else
  24. {
  25. alert("Not checked");
  26. return false;
  27. }
  28. }
  29.  
  30. }
  31. </script>
  32.  
  33.  
  34. </head>
  35. <body>
  36.     <form id=checkbox_form runat="server">
  37.         <p style="text-align: left">  
  38.             <asp:CheckBoxList ID="checkbox1" runat="server">
  39.                 <asp:ListItem>sheena</asp:ListItem>
  40.                 <asp:ListItem>sheenat</asp:ListItem>
  41.                 <asp:ListItem>sheenatt</asp:ListItem>                
  42.             </asp:CheckBoxList>&nbsp;</p>
  43.         <p style="text-align: left">
  44.             <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
  45.         </p>      
  46.  
  47.     </form>
  48. </body>
  49. </html>
  50.  

Newbie
 
Join Date: May 2007
Posts: 1
#8: May 18 '07

re: Checkboxlist Validation


Thanks buddies..

its been so useful to us....

Happy programming.. :)
Reply