Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check whether atleast one item is selected from the list 3

Status
Not open for further replies.

nubees

Programmer
Aug 6, 2003
39
US
where should place code to pop up an alert if user pushes submit with out selecting anything from the listbox.

Code:
<HTML>
<HEAD>
<TITLE>
Search PAGE
</TITLE>
<SCRIPT LANGUAGE="JavaScript">
         function getSelected(opt) {
            var selected = new Array();
            var index = 0;
            for (var intLoop = 0; intLoop < opt.length; intLoop++) {
               if ((opt[intLoop].selected) ||
                   (opt[intLoop].checked)) {
                  index = selected.length;
                  selected[index] = new Object;
                  selected[index].value = opt[intLoop].value;
                  selected[index].index = intLoop;
               }
            }
            return selected;
         }

         function outputSelected(opt) {
         
            var sel = getSelected(opt);
            
            var strSel = "";
            for (var item in sel)
               strSel += sel[item].value + "\n";
            alert("Selected Items:\n" + strSel);
         }      
      </SCRIPT>

</HEAD>
<BODY>
<select name="Logicalnm_lst" >        
<%        int i=0;
     while (iter_ln.hasNext()) {
     if(i==0) {%>
     <option value="selectlst">Select the Logical name
     <% i=1;
     }
       String value = (String)iter_ln.next();
%>
         <option value = "<%=value%>"><%=value%></option>
<%
     }    
%>
</select>   

<INPUT TYPE="submit" name="simplesearch" value="Submit" ONCLICK="outputSelected(this.form.Logicalnm_lst.options)">
 
well, the way you have it set up, there will always be one item selected from the list.

If you mean, how do you determine if the first option is selected, simply test for selectedIndex == 0 and then display an error message.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Are you looking for the position to test tjhis?
Ideally you would put it in the form tag:
Code:
<form ... onsubmit="return checkValid();">
and then your function would return true or false depending upon whether or not the form is finished:
Code:
function checkValid()
{
  if (all_elements_are_fine)
    return true;
  else
    return false;
}

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Thanks both of you for the input,

sorry if I didnot convey my qestion good enough.
There is a listbox in the form where atleast one item must be selected otherwise it should throw an error.

with the code I have right now. Even if I didnot select anything from the listbox I was able goto the next page but no results are showing up(because the selection is empty).

Am I worse?? or better this time?

clFlava I donot want to check whether the selected option is first one or not.

well, the way you have it set up, there will always be one item selected from the list.
if that is so, why is it not diplaying any alert button
when I simply hit submit button with out touching list box.

chessbot, sorry that is not what I am looking for. Hope this time I made some sense.

Am I worse?? or better??

 
if that is so, why is it not diplaying any alert button
when I simply hit submit button with out touching list box.

because the first option in the listbox is selected.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
You can always create a function like getSelectedIndex and have it return -1 if no index is selected. Here's an example of one I made with my code (very similar to what you've already done).

Code:
	function getSelectedCourseIndex()
	{
		var option = document.editForm.elements['courseOption'];
		var i;
		for(i = 0; i < option.length; i++)
		{
			if( (option[i].checked) )
			{
				return i;
			}
		}
		
		return -1;
	}

Then just make sure that it alerts the user if the function returns 1. Hope that helps.

Nick Ruiz
Webmaster, DBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top