Even without selecting the checkbox, a MsgBox still pops up...
In my code below, regardless of whether the user checks the checkbox or not, it will always redirect to Google: If the user selects the checkbox, then it redirects to www.google.com. However, if the user forgets to check the checkbox, a message box with an OK button appears. After clicking OK, it should redirect to www.google.com.
What I need is:
If a user forgets to select any checkboxes, display a MsgBox with an OK button and keep them on the same page. Otherwise, if the user does select any checkboxes, then redirect them to www.google.com.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CheckBox ID="CheckBox2" runat="server" />
</div;
<asp:Button ID="Button1" runat="server" OnClientClick ="return ConfirmSelection(this.form);" Text="Button" />
</form>
<script type="text/javascript">
function ConfirmSelection(frm)
{
for (i=0; i<=1; i++) {
//chkSubjectOfInterest is the id of your checkbox control
if (frm.elements[i].name.indexOf('chkSubjectOfInterest') != -1)
{
if (frm.elements[i].checked)
{
return true
}
}
}
alert('You haven\'t selected an item yet!')
return false;
}
</script>
</body;>
</html;>