Hi all.

I have some problems with radio buttons. Hope to get some help here. Thanks in advance.

Here's the html:

<?php while($row=mysql_fetch_array($result)) { ?>
         <input name="replacement" id="replacement" type="radio"
            value="<?php echo  $row['date']; echo $row['title']; ?>"

I need to validate if none of the radio buttons is checked using javascript. How can I do that? Any help will be appreciated. =)

for (i=0, n=theForm.radio_btn_name.length; i<n; i++) {
        if (theForm.radio_btn_name[i].checked) {
            var checkvalue = theForm.radio_btn_name[i].value;
            break;
        } 
    }

I've solved the problem using codes below:

function validateCB(theName){
   var counter=0;
   var cb=document.getElementsByName(theName)
   for (i=0; i<cb.length; i++) {
        if((cb[i].tagName=='INPUT')&&(cb[i].type=='radio')){
          if (cb[i].checked)
             counter++;
        }
   }
   if (counter==0) {  
   return false;
  }
return true;
}

function check(){

	
	if (!validateCB('replacement')) {
   		alert("Please fill in all the fields marked with *");
   		return false;
	 }
         return true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.