www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] checkbox validation

  1. #1
    Join Date
    Feb 2006
    Location
    india
    Posts
    680

    resolved [RESOLVED] checkbox validation

    Hi All,

    I am going to validating a checkbox which is created dynamically that means the field name is not fixed everytime .Here is my html code

    HTML Code:
    <tr>
    			<td class="Cat" bgcolor="#cccccc" style="padding-left:10px; border-bottom:1px solid #ffffff;">Computer			<span style="color:#fff;">*</span>		 
    			</td>
    			<td style="padding-left:10px;">
    									laptop			<input type="checkbox" name="n5_Computer[]" id="n5_Computer[]" value="laptop"  />
    
    			pc			<input type="checkbox" name="n5_Computer[]" id="n5_Computer[]" value="pc"  />
    			notebook			<input type="checkbox" name="n5_Computer[]" id="n5_Computer[]" value="notebook"  />
    			iphone			<input type="checkbox" name="n5_Computer[]" id="n5_Computer[]" value="iphone"  />
    										
    			</th>
    		  </tr>	
    		  	 
    		  <tr>
    			<td class="Cat" bgcolor="#cccccc" style="padding-left:10px; border-bottom:1px solid #ffffff;">Location			<span style="color:#fff;">*</span>		 
    			</td>
    
    			<td style="padding-left:10px;">
    									India			<input type="checkbox" name="n5_Location[]" id="n5_Location[]" value="India"  />
    			saudi			<input type="checkbox" name="n5_Location[]" id="n5_Location[]" value="saudi"  />
    			kolkata			<input type="checkbox" name="n5_Location[]" id="n5_Location[]" value="kolkata"  />
    										
    			</th>
    		  </tr>	
    and the javascript code is

    Code:
     var checkbox_choices = 0;
    						 
     						var getcid = "n5_"+en[i];
    						alert(getcid);
    						 var selection =document.getElementsByName(getcid).length
    					alert(selection);
    				 	// Loop from zero to the one minus the number of checkbox button selections
    					  for (var counter = 0; counter < selection; counter++) 
    					{
    					
    					// If a checkbox has been selected it will return true
    					// (If not it will return false)
    					if (document.form3[getcid][counter].checked)  
    					{ checkbox_choices = checkbox_choices + 1; }
    					
    					}
    					
    					 if(checkbox_choices == 0)
    					 {
    					 emptyfields += "\n - Please select :" + clarr[i];
    					 }
    The problem is always the checkbox_choices becomes 0 though if I select.


    I think the problem is in this line

    Code:
     var selection =document.getElementsByName(getcid).length
    Please suggest,

    Raj

  2. #2
    Join Date
    Feb 2006
    Location
    india
    Posts
    680
    Its solved.the code is

    Code:
    var getcid = "n5_"+en[i]+"[]";	  
    			   var grp = document.forms["form3"].elements[getcid];
    var x, len = grp.length;
    for(x=0; x<len; ++x) if(grp[x].checked) break;
    if(len<=x)
    {
    emptyfields += "\n - Please select :" + clarr[i]; 
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles