<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioButtonListValidation.aspx.cs" Inherits="Demo.RadioButtonListValidation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Validating
RadioButtonList using Javascript</title>
<script type="text/javascript">
function Validate()
{
var isChecked
= false;
var jVar1
= document.getElementById('<%=RadioButtonList1.ClientID %>');
var radio
= jVar1.getElementsByTagName("input");
for (var i
= 0; i < radio.length; i++) {
if (radio[i].checked
== true) {
isChecked
= true;
alert(radio[i].value);
}
}
if (!isChecked)
{
alert("Please
select at least one Item.");
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Vertical">
<asp:ListItem Value="0">None</asp:ListItem>
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
</asp:RadioButtonList>
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return
Validate();"/>
</div>
</form>
</body>
</html>
No comments:
Post a Comment