SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    United states
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check whether atleast one item is selected from the list

    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)">
    please help,
    nubee

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, your Select and the Submit button have to be in a form--it looks like you need to review some basic html first. In any case, you can put an onsubmit event handler in the <form> tag, which can call a function. In the function, you can check to see whether anything was selected, and if nothing was selected, you can alert() a message, and return false from the function to cancel the submit.

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Nebraska
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    Use something like this:

    Code:
    <html>
    <head>
    <title>Fruit Form</title>
    <script language="javascript1.2">
    
    function checkForm(){
         if(document.fruitForm.selectFruit.value=="none"){
    	      alert("You must choose an option from the list.");
    		  return false;
    	 }
    	 else
    	      return true;
    }
    
    </script>
    </head>
    
    <body>
    <form name="fruitForm" onSubmit="return checkForm();" method="post" action="">
      <select name="selectFruit">
        <option value="none">Select One...</option>
        <option value="banana">Banana</option>
        <option value="orange">Orange</option>
        <option value="grape">Grape</option>
        <option value="apple">Apple</option>
      </select>
    </form>
    </body>
    </html>
    Louie


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •