0

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.

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Pgmer
0

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.

0

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

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

0

Thanks for ur reply.

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

0

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>
0

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;
        }
    }

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.