Results 1 to 4 of 4

Thread: [RESOLVED] checkbox list ,requried field validator

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Resolved [RESOLVED] checkbox list ,requried field validator

    see the code

    Code:
    <div id="list" class="right_txt_space_accm">
    											<table width="100%" border="0" cellspacing="0" cellpadding="0">
    											  <tr>
    												<td  align="left"><strong></strong>
                                                        <asp:CheckBoxList ValidationGroup="testGroup" RepeatColumns=3 RepeatDirection=Horizontal ID="CheckBoxList1" runat="server">
                                                            <asp:ListItem Value="1">Cutomer Broker</asp:ListItem>
                                                            <asp:ListItem Value="2">N.V.O.C.C</asp:ListItem>
                                                            <asp:ListItem Value="3">Hauloiers</asp:ListItem>
                                                            <asp:ListItem Value="4">Air Freight</asp:ListItem>
                                                            <asp:ListItem Value="5">Sea Freight</asp:ListItem>
                                                            <asp:ListItem Value="6">Supply Chain Management</asp:ListItem>
                                                            <asp:ListItem Value="7">Groupage Services</asp:ListItem>
                                                            <asp:ListItem Value="8">Project Cargo</asp:ListItem>
                                                            <asp:ListItem Value="9">Ware House Operator</asp:ListItem>
                                                        </asp:CheckBoxList>
                                                        </td>
    												
    											  </tr>
                                                    
    											</table>
    										</div>
    										<div style="display:none;">
    										<table>
    										<tr>
    										<td> 
    										  <asp:TextBox ID="txtcheckbox"  ValidationGroup="testGroup"  runat="server"></asp:TextBox>
                                                        <asp:RequiredFieldValidator ID="valCheckboxlist" runat="server" ValidationGroup=testGroup EnableClientScript=true ControlToValidate =txtcheckbox  ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
                                                 </td>
    										</tr>
    										</table>
    										</div>
    and script is as
    Code:
     <script type="text/javascript">
           
    
               $(function() {
               function checkboxclicked() {
               var n = $("# list input:checked").length);
               $("input:#txtcheckbox").val(n==0 ? "" : n);
               }
               $(":checkbox").click(checkboxclicked) ;        
               
                         
               });
               
           }</script>
    i need to validate the checkbox list(at least one checkbox is selected but its not doing any thing) ,can any one help me
    There is no achievement without goals

  2. #2
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: checkbox list ,requried field validator

    sir, I use something like this, and I find it very useful especially when getting IDs from a database

    here it is f you like

    HTML Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function checkIfSelected(lastone)
    {
    	var boxes = "box" + 0;
    	var isChecked = false;
    	for (var i = 1; i <= lastone; i++){
    		boxes = "box" + i;
    		if (document.getElementById(boxes)){
    			if (document.getElementById(boxes).checked == true){
    				isChecked = true;
    			}
    		}
    	}
    	if (isChecked == false){
    		window.alert('check at least one please');	
    	}
    }
    </script>
    </head>
    <body>
    
    <form>
    <input type="checkbox" id="box1" />
    <input type="checkbox" id="box2" />
    <input type="checkbox" id="box3" />
    <input type="button" onclick="checkIfSelected(3);" value="check" />
    </form>
    
    </body>
    </html>

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: checkbox list ,requried field validator

    well i did like this

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
         <script type="text/javascript">
    
             $(function() {
                 $('#<%= btn_submitt.ClientID %>').click(function() {
    
                     var checkedControls = $('#<%= CheckBoxList1.ClientID %>').find('input:checkbox:checked');
                     var selected = jQuery('#<%= Rdbtn_hotelbook.ClientID %> input:checked').val();
    
                     if (checkedControls.length > 0) {
                         //  alert('Valid');
                        // return true;
                     }
                     else if (selected == 0) {
                         alert('u selected yes');
    
                     }
                     else {
                         alert('Please select atleast one checkbox');
                     }
    
                 });
    
    
                 return false;
    
    
             });    
           
           </script>
    but its since i need to check radio button too its selected or not . please correct or guide me .
    There is no achievement without goals

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: checkbox list ,requried field validator

    do you just need to check if both conditions are met? I'm not sure I understand what you're asking for.

    Code:
    if(checkedControls.length > 0 && selected == 0){
    
      // do something
    
    }else{
      alert("You must selected at least one checkbox");
    }
    Like Archer? Check out some Sterling Archer quotes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width