Connecting Tech Pros Worldwide Forums | Help | Site Map

Checkbox validation problem...??

Newbie
 
Join Date: Jan 2009
Posts: 23
#1: Jun 18 '09
Error in client side validation
object expected error in this line,--> OnClientClick="return validation1(<%=maxnumber %>);
Syntax error : in this line :for(i=0;i<maxno;i++)

i want to validate atleast a single checkbox to be selected ...

also i dont know how to do server side side validation for this generated checkboxes ....and then need to display the selected checkbox after validation.....??

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script language="javascript"> 
  3. function validation1(maxno)
  4. {
  5. var val1;
  6. alert(sda);
  7. for(i=1;i<maxno;i++)
  8. {
  9. if(document.getElementById('chk_'+i).checked)
  10. val=1;
  11. }
  12. if(val==0)
  13. {
  14. alert("Select atleast 1")
  15. return false;
  16. }
  17.  </script>
  18. </head>
  19. <body>
  20. ------fewcodes for conxn and other stuff......
  21. <%
  22. dim maxnumber as integer = dr.item("mxno")
  23.  For i As Integer = 1 To maxnumber %>
  24.    <input type="checkbox" id="chk_<%=i%>" name="chk_<%=i %>" /><%=i%>
  25. <%  End If
  26. Next %>
  27. <asp:Button ID="chkbtn" runat="server" text="continue" OnClientClick="return validation1(<%=maxnumber %>);"/>
  28. </body>
  29.  
  30.  

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#2: Jun 26 '09

re: Checkbox validation problem...??


The reason you're getting the syntax error is because you haven't enclosed the value you're passing as a parameter in quotes.

You come from a classic ASP background don't you.
There is an ASP.NET CheckBoxList control that I think you'll be interested in.

If you use an ASP.NET CheckBoxList control (instead of the HTML input type=checkbox elemetn) you can access it in your VB.NET code.

For example, if you wanted to check (in your server code) if any of the checkboxes were checked in the CheckBoxList you would check to see if the SelectedIndex property was greater than 0:
Expand|Select|Wrap|Line Numbers
  1.  
  2. If CheckBoxList1.SelectedIndex > 0 Then
  3.   'A checkbox was checked, it returns -1 if none were checked
  4. End If
So, that's how you would check server side if any of the CheckBoxes in a CheckBoxList control were checked.

If you want to validate client side it's a little trickier. But first thing's first: if you want to use ASP.NET, then strongly consider using the features that it provides you with.

-Frinny
Reply