Validating Radio Buton Clearing the Selected value by clicking clear button using Java Script

Self-Innovator
Posted by Self-Innovator under JavaScript category on | Points: 40 | Views : 2006
.aspx Code
<table>
<tr>
<td><asp:Label ID="lblGender" runat="server" Text="Gender"></asp:Label></td>
<td style="width: 197px">
<asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Male" Value="1"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>

Javascript Function
<script language="javascript" type="text/javascript">
function Validate()
{
var Gender=document.getElementById("rblGender");
var flag=0;
for(i=0;i<Gender.cells.length;i++)
{
if(Gender.cells[i].firstChild.checked)
{
flag=1;
break;
}
}
if(flag==0)
{
alert("Please choose gender");
return false;
}
}

function Clear()
{
var Gender=document.getElementById("rblGender");
for(var i=0;i<rblGender.cells.length;i++)
{
if(Gender.cells[i].firstChild.checked)
{
Gender.cells[i].firstChild.checked=false;
}
}
}


.cs

Page_load()
{
btnSubmit.Attributes.Add("onClick", "return Validate()");
btnClear.Attributes.Add("onClick", "Clear()");
}

Comments or Responses

Posted by: T.saravanan on: 12/26/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag.

Note:
1. Provide the proper Title for your code snippet then only user easily to identitfy your code.
2. In our DNF website Square brace of 'i' like ["i"] is consider as "Italic" format. So use other chars instead of 'i'



Login to post response