Friday, January 22, 2010

Javascript Radio Button validation with function

Function to check a radio button if checked or not
function chkRadio(radio)
{
 var i, flg=0;
 
 for(i=0; i<radio.length; i++)
 {
  if(radio[i].checked)
   flg=1;
 }
 
 return flg;
}

Usage:
if(!chkRadio(document.myForm.group1))
{
 alert("Please select");
 return false;
}
.
.
<form name="myForm" .....
.
<input type="radio" value="V1" name="group1">
<input type="radio" value="V2" name="group1">
<input type="radio" value="V3" name="group1">

No comments: