Home » VB.NetRSS

radiobutton validation using javascript

hi' i have two radiobuttons and a button.On button click i have to validate whether the radiobutton is selected or not. I tried the following,but it is not working.pls help.


<asp:RadioButton  runat="server" ID="rbyes" Text="Yes"  visible="true" GroupName="a"  />
 <asp:RadioButton  runat="server" ID="rbno" Text="No" visible="true" GroupName="a" />
 <asp:Button  ID="btnOk" runat="server" Text="OK" OnClick="btnOk_Click"  OnClientClick="return selectRadioButton();"/>



function selectRadioButton()
{

var y=document.getElementById("ctl00_ContentPlaceHolder1_rbyes");
var n=document.getElementById("ctl00_ContentPlaceHolder1_rbno");


if(y.checked==false && n.checked==false)
{
alert(Please select Yes/No");
return false;
}
return true;
}

 

4 Answers Found

 

Answer 1

Use a RadioButtonList and a RequiredFieldValidator.

http://forums.asp.net/t/1199803.aspx

 

Answer 2

Hi,

Your code is working  fine for me. Can u please say do u get any errors in this code?

ans also I dont know whether its a typo please check that u have missed a apostrophe in the alert message. 

 

Answer 3

<asp:RadioButton  runat="server" ID="rbyes" Text="Yes"  visible="true" GroupName="a"  />
 <asp:RadioButton  runat="server" ID="rbno" Text="No" visible="true" GroupName="a" />
 <asp:Button  ID="btnOk" runat="server" Text="OK" OnClick="btnOk_Click"  OnClientClick="return selectRadioButton();"/>



function selectRadioButton()
{

var y=document.getElementById("<%=rbyes.ClientID%>");
var n=document.getElementById(""<%=rbno.ClientID%>");


if(y.checked==false && n.checked==false)
{
alert(Please select  Yes/No");
return false;
}
return true;
}


 

Answer 4

hi Divya,

you have written code correct but you have done small mistakes i have changed those mistakes check this it's working  fine

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
    function  selectRadioButton()
{

var y=document.getElementById("<%=rbyes.ClientID %>");
var n=document.getElementById("<%=rbno.ClientID %>");


if(y.checked==false && n.checked==false)
{
alert("Please select  Yes/No");
return false;
}
return true;
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:RadioButton  runat="server" ID="rbyes" Text="Yes"  visible="true" GroupName="a"  />
 <asp:RadioButton  runat="server" ID="rbno" Text="No" visible="true" GroupName="a" />
 <asp:Button  ID="btnOk" runat="server" Text="OK" OnClientClick ="return selectRadioButton();"/>
    </form>
</body>
</html>



 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter