PDA

View Full Version : Radio button validation using javascript



Muneeb
17 Sep 2010, 01:24 PM
below is the javascript code for radio button validation which is not working properly can some tell me what is the problem !!!

i get the error of document .form1.group1[i] is not defined
----------------------------------------------------------------------------


<script type="text/javascript">
function validate()
{
if(true)
{
var chosen=false;
var len=document.form1.group1.length;

var i;
for(i=0; i<=len; i++)
{
document.write(i);
if(document.form1.group1[i].checked)
{
document.write("in if");
chosen=true;
}
}
if(!chosen)
{
alert("select the property type");
return false;
}

}
else
{
alert("in else");
return false;
}
}
</script>

<form method="post" name="form1" onsubmit="return validate()">
<div class="textboxaddproperty">
<select name="propertytype" id="propertytype">
<option value="plot">Plot</option>
<option value="Commercial">Commercial</option>
<option value="house">House</option>
</select>
<input type="radio" value="Sale" name="group1"/>
Sale
<input type="radio" value="rent" name="group1"/>
Rent </div>

<div style="clear:both"></div>
<div class=" label"></div>
<div class="textbox">
<input type="submit" name="submit" value="Submit" />

</form>

chintan
21 Sep 2010, 06:53 AM
Try

if(document.form1.group1[i].checked=='true')

Hope it will work.