Hi Guys I'm New to web development.

i need one help. I'm having list box on my form, in form load event i will bind the list box with database value. user should be able to select only 5 items from the list box and also i want to validate onsubmit button click weather user is selected 5 items or not . if not give alert. client side java-script i need to use? sample code will help.

Tanks.

You can do so by adding AJAX - updatepanel. Map the button's click with trigger of updatepanel where you can get the count of selected items.

Hi Guys I'm New to web development.

i need one help. I'm having list box on my form, in form load event i will bind the list box with database value. user should be able to select only 5 items from the list box and also i want to validate onsubmit button click weather user is selected 5 items or not . if not give alert. client side java-script i need to use? sample code will help.

Tanks.

Hi,
I am not sure if this will help but here is a form Validate code to check if all field have entries.

<script language="JavaScript"><!--
function Form_Validator(theForm){
	if (theForm.Name.value=="") 
	{
      	alert("Please enter your Name.")
    	theForm.Name.focus()
    	return false
	}

	if (theForm.Address.value==""){
      	alert("Please enter your Address.")
    	theForm.Address.focus()
    	return false
	}	
	
	if (theForm.PostCode.value==""){
      	alert("Please enter your Post Code.")
    	theForm. PostCode.focus()
    	return false
	}
		
	
	if (theForm.Email.value==""){
      	alert("Please enter your E-Mail Address.")
    	theForm.Email.focus()
    	return false
	}
	
	if (theForm.Message.value==""){
      	alert("Please enter your Message.")
    	theForm.Message.focus()
    	return false
	}
}
-->
</script>

Above Code Called from Form onSubmit

<form method="POST" action="YourPage.asp" onSubmit="return Form_Validator(this)" name="theForm">  Rest of your Form </Form>

Hope this helps
Doreenpen

Thanks for ur reply.

i think the above code will not help.
i need to prevent user from not selecting more than 5 items.

Try this.

<head runat="server">
    <script type="text/javascript">
        function ValidateForm() {
            var t = document.getElementById("lst1");
            var count = 0;
            for (i = 0; i < t.length; i++) {
                if (t.item(i).selected)
                    count++;
            }
            if (count < 5)
                return false;
            return true;       
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="return ValidateForm()">
    <div>
     
      <asp:ListBox SelectionMode="Multiple" ID="lst1" runat="server" Height="95px" Width="86px">
         <asp:ListItem Value="1" Text="Test1"/>
         <asp:ListItem Value="2" Text="Test2"/>
         <asp:ListItem Value="3" Text="Test3"/>
         <asp:ListItem Value="4" Text="Test4"/>
         <asp:ListItem Value="5" Text="Test5"/>
         <asp:ListItem Value="6" Text="Test6"/>
         <asp:ListItem Value="7" Text="Test7"/>
         <asp:ListItem Value="8" Text="Test8"/>
         <asp:ListItem Value="9" Text="Test9"/>
      </asp:ListBox>
      <asp:Button ID="btn1" runat="server" Text="Submit" />
    </div>
    
    </form>
</body>

Hi thanks for you reply.

i found the solution for that. here is the code.

<asp:ListBox ID="lstbox" runat="server" SelectionMode="Multiple" 
               AutoPostBack="true" CausesValidation="true" OnSelectedIndexChanged="lstbox_SelectedIndexChanged1"  >
             <asp:ListItem Text="one" Value="1"></asp:ListItem>
       <asp:ListItem Text="two" Value="2"></asp:ListItem>
       <asp:ListItem Text="three" Value="3"></asp:ListItem>
       <asp:ListItem Text="Four" Value="4"></asp:ListItem>
       <asp:ListItem Text="Five" Value="5"></asp:ListItem>
       </asp:ListBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="Please select 3 items only."
         OnServerValidate="CustomValidator1_ServerValidate" >
        </asp:CustomValidator>

Code behind:

protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs args)
    {
        int count = 0;

        for (int i = 0; i < lstbox.Items.Count; i++)
        {

            if (lstbox.Items[i].Selected)

                count++;

        }
        if (count <= 2)
        {
            args.IsValid = true;
        }
        else
            args.IsValid = false;
            

    }

  
   
    protected void lstbox_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int count = 0;
        int i = 0;
        System.Web.UI.WebControls.ListBox sen = default(System.Web.UI.WebControls.ListBox);
        sen = (System.Web.UI.WebControls.ListBox)sender;
        for (i = 0; i <= lstbox.Items.Count - 1; i++)
        {
            if (lstbox.Items[i].Selected == true)
            {
                count = count + 1;
            }
        }
        if (count > 3)
        {
            lstbox.Items[sen.SelectedIndex].Selected = false;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.