473,513 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating a listbox using javascript

6 New Member
can anybody tell me how do u validate a listbox to select atleast one item from it. I have a listbox within the template field of a grid view. and these listboxes can vary from 1 to 4. how can i know how many listboxes are present so that i can loop in javascript.
Dec 24 '08 #1
7 14974
acoder
16,027 Recognized Expert Moderator MVP
Welcome to Bytes!

I assume you mean a multiple select. You can validate it by looping over the options (see example) and checking how many are selected (check the selected property).

To get all the list boxes in a page, use document.getElementsByTagName("select") and loop over the resulting array.
Dec 24 '08 #2
laks2886
6 New Member
thanks for the reply.. i have drop down and lsit boxes in my page.. these listboxes are dynamic and can vary from 1 to 4.

as u said i have used document.getElementsByTagName("select") to get all the references.. this is working fine for the dropdown, when it comes to list box ... instead of selecting the labeldesc of the lisbox it takes the values of the list box.
Dec 25 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
Can you show the code for the list box. How does it differ from a drop-down?
Dec 29 '08 #4
laks2886
6 New Member
This is the Java script functin for validating the drop down.. listbox n text box...

Expand|Select|Wrap|Line Numbers
  1. function ValidateSave()
  2. {
  3.     var lblSelectClient                 = document.getElementById('<%=ddlSelectClient.ClientID%>');
  4.     if(lblSelectClient.selectedIndex==0)
  5.     {
  6.         alert('Please select a client');
  7.         return false;
  8.     }
  9.  
  10.     var ddlSelectHierarchy             = document.getElementById('<%=ddlSelectHierarchy.ClientID%>');
  11.     if(ddlSelectHierarchy.selectedIndex==0)
  12.     {
  13.         alert('Please select a hierarchy');
  14.         return false;
  15.     }    
  16.  
  17.     var hdMode                          = document.getElementById('<%=hdMode.ClientID%>');
  18.     var isSelected                      = false;
  19.     var hasList                         = false;
  20.     var strLevel="";
  21.     //if(hdMode.value="HierEdit")
  22.     {
  23.         var dropdownReferences = document.getElementsByTagName("select");
  24.  
  25.         if(dropdownReferences !=null)
  26.         {
  27.             if(dropdownReferences.length != null)
  28.             {
  29.                 for (i = 0; i <dropdownReferences.length-1; i++)
  30.                 {
  31.                     if(dropdownReferences[i].id.search("LstHierA")==-1)
  32.                     {
  33.                         if(dropdownReferences[i].selectedIndex==0)
  34.                         {
  35.                                  var level = document.getElementById(replace(dropdownReferences[i].name.replace("ddlLevel","lblLevelDesc"),"$","_")).innerText;
  36.                                 if(level.search("Select")!=-1)
  37.                                 {
  38.                                     level=level.replace("Select ","");
  39.                                 }
  40.  
  41.                                 alert('Please select a value for '+level);
  42.                                 return false;                     
  43.                         }
  44.                     }
  45.                     else
  46.                     {
  47.                         hasList = true;
  48.                         strLevel = document.getElementById(replace(dropdownReferences[i].name.replace("lstHierA","lblLevelDesc"),"$","_")).innerText;
  49.                         if(strLevel!="")
  50.                         {
  51.                             strLevel=strLevel.replace("Select ","");
  52.                         }
  53.                         for(j=0;j<dropdownReferences[i].length;j++)
  54.                         {
  55.                             if(dropdownReferences[i].options[j].selected) 
  56.                             {
  57.  
  58.                                 isSelected=true;
  59.                                 break;
  60.                             }
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.  
  67.         var inputReferences= document.getElementsByTagName("input");
  68.  
  69.            if (inputReferences != null)
  70.         {
  71.             if (inputReferences.length != null)
  72.             {
  73.                 for (i = 0; i <= inputReferences.length-1; i++)
  74.                 {
  75.                     if (inputReferences[i].type == 'text')
  76.                     {
  77.                         if (inputReferences[i].value == null || inputReferences[i].value == "")
  78.                         {
  79.  
  80.                              var level = document.getElementById(replace(inputReferences[i].name.replace("txtEnterNewLevel","lblLevelDesc"),"$","_")).innerText;
  81.                             if(level.search("Select")!=-1)
  82.                             {
  83.                                 level=level.replace("Select ","");
  84.                             }
  85.                             else
  86.                             {
  87.                                 level = document.getElementById(replace(inputReferences[i].name.replace("txtLeafNode","lblLevelDesc"),"$","_")).innerText;
  88.                                 if(level.search("Enter")!=-1)
  89.                                 {
  90.                                     level=level.replace("Enter ","");
  91.                                 }
  92.                             }
  93.  
  94.                             alert('Please enter a value for '+level);
  95.                             inputReferences[i].focus();
  96.                             return false;
  97.  
  98.                         }
  99.                         var Ok;
  100.                         OK=CheckLength(inputReferences[i]);
  101.                         if(!OK)
  102.                         {
  103.                             return false;
  104.                         }
  105.                     }
  106.                 }            
  107.             }             
  108.         }
  109.     }
  110.     if(hasList)
  111.     {
  112.         if(!isSelected)
  113.         {
  114.             if(strLevel!="")
  115.             {
  116.                 alert('Please select a '+strLevel);
  117.                 return false;
  118.             }
  119.         }
  120.     }
  121.  
  122.     return true;
  123. }
  124.  
  125.  
lstHierA is the name of the listbox
Dec 30 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
OK, but what about the actual list box code (view source at the client to see how it's rendered in HTML).
Dec 31 '08 #6
laks2886
6 New Member
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="grdLevels" runat="server" AutoGenerateColumns="False" Width="750px" OnRowDataBound="grdLevels_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" EnableViewState="true">
  2.                 <Columns>
  3.                     <asp:TemplateField HeaderText="LevelDesc" ShowHeader="False">
  4.                         <ItemTemplate>
  5.                             <asp:Label ID="lblLevelDesc" Width="150px" runat="server" Text='<%# Bind("LevelDesc") %>'></asp:Label>
  6.                         </ItemTemplate>
  7.                     </asp:TemplateField>
  8.                     <asp:TemplateField ShowHeader="False" HeaderText="LevelControls">
  9.                         <ItemTemplate>
  10.                             <asp:DropDownList ID="ddlLevel" Width="200px" Enabled="false" OnSelectedIndexChanged="ddlLevel_SelectedIndexChanged" runat="server" AutoPostBack="true" AppendDataBoundItems="true">
  11.                             <asp:ListItem Selected="True" Value="0">--Select--</asp:ListItem>
  12.                             </asp:DropDownList>
  13.                             <asp:TextBox ID="txtLeafNode" runat="server"></asp:TextBox>
  14.  
  15.  
  16.  
  17.                            <div id='divList1' runat="server"  style="Z-INDEX: 102;OVERFLOW:auto; WIDTH: 247px;height:90px" >
  18.  
  19.                             <asp:ListBox ID="LsthierA" SelectionMode="Multiple" Width="700px" runat="server" Rows="5" Height="10000">
  20.                             </asp:ListBox>
  21.  
  22.                             </div>
  23.  
  24.  
  25.                              <asp:ListBox ID="lstLevel" Width="200px" Visible="false" runat="server"></asp:ListBox>
  26.                             <asp:HiddenField ID="hdlLevelKey" Value='<%#DataBinder.Eval(Container.DataItem,"LevelKey") %>' runat="server" />
  27.                             <asp:HiddenField ID="hdIsLeafNode" Value='<%#DataBinder.Eval(Container.DataItem,"IsLeafNode") %>' runat="server" />
  28.                             <asp:HiddenField ID="hdIsRoot" Value='<%#DataBinder.Eval(Container.DataItem,"IsRoot") %>' runat="server" />
  29.                             <asp:HiddenField ID="hdDDLSelection" runat="server"/>
  30.                             <asp:HiddenField ID="hdHierKey" runat="server"/>
  31.  
  32.                             <asp:TextBox ID="txtEnterNewLevel" Width="195px" Visible="false" runat="server" Enabled="false"></asp:TextBox>
  33.                         </ItemTemplate>                      
  34.                     </asp:TemplateField>
  35.                     <asp:TemplateField HeaderText="NewLevelButton">
  36.                         <ItemTemplate>
  37.                             <asp:Button ID="btnNewLevel" OnClick="btnNewLevel_Click" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"LevelDesc") %>' />
  38.                         </ItemTemplate>
  39.                     </asp:TemplateField>
  40.                     <asp:TemplateField HeaderText="AttributeValueButton">
  41.                     <ItemTemplate>
  42.                         <asp:Button ID="btnAtVal" runat="server" Text="Add Attribute Value" OnClick="btnAtVal_Click" Width="118px" />
  43.                     </ItemTemplate>
  44.                     </asp:TemplateField>
  45.  
  46.                 </Columns>
  47.                 <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
  48.                 <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
  49.                 <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
  50.                 <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
  51.             </asp:GridView>



__________________________________________________ _ Is this wat u asked for??? In the function validatesave() .. am getting the references of the items with tag select. which includes dropdowns and listboxes which vary from (0-4). 1. i want to know how many listboxes are present?? wat's happening is .. whenever i select a item(eg., ABCD) from the listbox select item... it's giving an pop up alert saying.. Please select a value for ABCD. Actually validation should happen when am not selecting from the listbox and it should be like ' Please selec a value for Item'? Please let me know wat is wrong in function validate save?
Jan 10 '09 #7
acoder
16,027 Recognized Expert Moderator MVP
@laks2886
Almost, but not quite. What you've posted is ASP.NET code, which is not much use for JavaScript. JavaScript runs on the client-side, so it sees the client-side generated code which you should post (load the page and view source in the browser).

PS. please remember to use [code] tags when posting code. Thanks!
Jan 10 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2904
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
1
9851
by: Daniel | last post by:
hi, I had an asp:listbox, and everytime i click item inside, the bar automatically go to the top, is there any way to keep the scroll position? I turn on the smartNavigation, it still doesn't...
4
5088
by: Bob P. | last post by:
Hello, I have a page with: * two side-by-side asp:listboxes and two arrow asp:buttons allowing users to add/remove email addresses between them -- very much like Outlook, where you have the...
0
1568
by: deepak | last post by:
Hi all, i m transefering values from one listbox to another listbox using javascript(both are html controls), the first listbox is populating with database at page_load time and the second listbox...
6
5181
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
4
2783
by: Jeff | last post by:
....still new at this. ...hopefully a simple question Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on each...
4
9369
by: Dave | last post by:
I recently found out that the RequiredFieldValidator works differently for a ListBox than I needed; it checks to see if a row is selected, rather than checking to see if the listbox is empty. I...
7
5581
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
15
2341
by: Doogie | last post by:
I have a .net app that a user currently enters a number in a text box, hits a button and a data call is executed. She wants the ability to enter in multiple numbers (up to 100). So to make...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.