Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to validate checkboxlist for atleast a single item to be selected..??

Posted on 2009-03-30
4
Medium Priority
?
519 Views
Last Modified: 2013-11-07
I need to validate for atleast a single checkboxlist is to be selected using Javascript not by required field validator..
.and also do the same in server side validation....i binded the checkboxlist..plz sugest

<asp:CheckBoxList ID="CheckBoxList1" runat="server"  RepeatColumns="2">
        </asp:CheckBoxList>&nbsp;

Open in new window

0
Comment
Question by:Rajar Ahmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 24017731
Try like this
ASPX
 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function Validate()
        {
            var checkBoxes = document.getElementsByTagName("input");
            var isValid = false;
            for(var i=0;i<checkBoxes.length;i++)
            {
                if(checkBoxes[i].type=="checkbox" && checkBoxes[i].id.indexOf("CheckBoxList1")!=0)
                {
                    isValid = true;
                    break;
                }
            }
            if(!isValid)
            {
                alert("Please select atleast one value");
            }
            return isValid;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"  RepeatColumns="2">
        </asp:CheckBoxList>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return Validate();" />
    </form>
</body>
</html>
 
 
CODEBEHIND
 
 
Partial Class Default2
    Inherits System.Web.UI.Page
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CheckBoxList1.DataSource = System.IO.Directory.GetFiles("C:\\")
            CheckBoxList1.DataBind()
        End If
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If CheckBoxList1.SelectedIndex = -1 Then
            Response.Write("Nothing is selected")
        End If
 
    End Sub
End Class

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24017972
server side is fine ,,.. but clientside i get message of 'Please select atleast one value' even if checkbox is selected...
 
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 24018031
Sorry the script should be like this
    <script type="text/javascript">
        function Validate()
        {
            var checkBoxes = document.getElementsByTagName("input");
            var isValid = false;
            for(var i=0;i<checkBoxes.length;i++)
            {
                if(checkBoxes[i].type=="checkbox" && checkBoxes[i].id.indexOf("CheckBoxList1")==0 && checkBoxes[i].checked)
                {
                    isValid = true;
                    break;
                }
            }
            if(!isValid)
            {
                alert("Please select atleast one value");
            }
            return isValid;
        }
    </script>

Open in new window

0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31564273
Thanks Mate...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question