Multiple select box, pop up question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #16
    When you say it's not working, what's happening instead?

    Comment

    • bonneylake
      Contributor
      • Aug 2008
      • 769

      #17
      Originally posted by acoder
      When you say it's not working, what's happening instead?
      Hey Acoder,

      Right now what happens is when i test it with no checkbox check an i click submit i get the error for the checkbox. So then i click the checkbox an click submit again an i get the same error. its like its not getting past checkbox to get to the selected box. i am thinking maybe i didn't get something closed out on my validation page but i am not sure what since i looked it over an it looked ok. here is what i got

      Code:
      function validate_form()
      {
      //VALIDATES TYPE OF HARDWARE FAILURE
      var hardware = document.getElementById('hardwarefailure')
      var tag = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
       //Rachels Note (had to use name== instead of id because id is unique and the select each item does not need to be unique)
      if(tag[i].name=="hardwarefailure" && tag[i].value =='')
      {
      	alert ('Please Select Type of Hardware Failure');
      	return false;
      }
      }
      //VALIDATES DEPT/VENDOR RESPONSIBILTYS
      var dept = document.getElementById('deptvendor')
      var tag2 = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag2.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
      if(tag2[i].name=="deptvendor" && tag2[i].value =='')
      {
      	alert ('Please Select Dept/Vendor Responsibility');
      	return false;
      }
      }
      //VALIDATES HAVE ALL PARTS BEEN RETURNED
      var parts = document.getElementById('partsreturn')
      var tag3 = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag3.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
      if(tag3[i].name=="partsreturn" && tag3[i].value =='')
      {
      	alert ('Please Select Have all parts been returned');
      	return false;
      }
      }
      
      //if(softhardware.value == ''){
      //softhardware.value = 'NOW IT IS THIS' ;
      //return false;
      //}
      //
      //if (document.userForm.assignees.checked == false)
      //    {
      //       alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
      //        return false; 
      //    }
      	
      
      
      
      var chkbox = document.getElementById('assignees');
      if (chkbox.checked) {
      }else {
      	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
              return false;
      }
      
      var selected = document.getElementById('toBox')
      var isSelected = false;
      for(var i=0; i<=selected.options.length-1; i++){
      if(selected.options[i].selected ){	
      isSelected = true;
      break;
      }	
      }
      if (!isSelected ){
      	alert ("Please Select a assignee");
      	return false;}
      	
      
      
      return true;
      }
      but basically the checkbox checked or not i still get an error. even when i put people in the selected box i still got the error.

      Thank you,
      Rach

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Hmm, not sure why it's not working. It may have something to do with how the toBox options are generated. Do you select them after moving the options from the fromBox?

        Comment

        • bonneylake
          Contributor
          • Aug 2008
          • 769

          #19
          Hey Acoder,

          I figured out the problem. it was having a problem with the check box. However, i still have a problem. here is what i got as of now.

          Code:
          if (document.userForm.assignees.checked == true)
             {
             }else  {alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
            return false; }
          	
          
          
          var selected = document.getElementById('toBox')
          var isSelected = false;
          for(var i=0; i<=selected.options.length-1; i++){
          if(selected.options[i].selected ){	
          isSelected = true;
          break;
          }	
          }
          if (!isSelected ){
          	alert ("Please Select a assignee");
          	return false;}
          
          
          return true;
          }
          the problems i am having is this. The form needs to still send even if an assignee is not selected an a check box is not selected (both of those together must not be selected for it to still send) if a check box is selected but no assignee it needs to bring an error an if a assignee is selected an a check box is not selected it needs to bring up an error.An the other problem i am having is when the errors come up the first problem i get is this. it first asks me to select the check box. i select the check box. then it asks me to select an assignee. i then select an assignee. it then asks me again to select an assignee again. an i found out the only way to prevent that error from continuing to come up is if i deselect the check box an reselect the checkbox. Anyway to accomplish all this?

          Thank you,
          Rach
          Last edited by bonneylake; Sep 4 '08, 05:42 PM. Reason: Kinda Fixed it

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            To accomplish the first part, you need to make the toBox validation check in the if statement, e.g.
            Code:
            if (document.userForm.assignees.checked) {
                var selected = document.getElementById('toBox')
                var isSelected = false;
                for(var i=0; i<=selected.options.length-1; i++){
                    if(selected.options[i].selected ){	
                        isSelected = true;
                        break;
                    }	
                }
                if (!isSelected ){
            	alert ("Please Select a assignee");
            	return false;
                }
            }
            I think it shouldn't matter if some options are selected and the checkbox is not selected. You can easily make a check on the server-side to avoid sending emails if the assignees checkbox hasn't been checked.

            Comment

            • bonneylake
              Contributor
              • Aug 2008
              • 769

              #21
              Hey Acoder,

              i hear what your saying but i am trying to get an error if the assignees is selected and checkbox not an vis versa because i know that if i don't bring up an error for either situation an that form gets sent an someone don't get there email that it will come back on me an they will tell me to add this in later (we don't got the brightest tools in the tool box). i know its a pain an don't seem practical. normally i just apply it to the checkbox where if they didn't select the check box it don't send. but knowing my luck they will select the check box an forget to select who they need to send to.

              but is there anyway to do all 3?

              Thank you :),
              Rach

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                In that case, call that function in the form validation function rather than onclick of the checkbox. Call it after the form passes validation, i.e. just before return true.

                As for the validation both ways of the checkbox and the select element, just check for the checked property and the selected property without any alert just be setting a variable, i.e. isChecked/isSelected. Then combine the two, i.e. if isChecked is true, but isSelected is not, then there's an error and also vice versa, but if both are true or both are false, it's OK.

                Comment

                • bonneylake
                  Contributor
                  • Aug 2008
                  • 769

                  #23
                  Hey Acoder,

                  i got the onclick working by putting onclick in the form. so every time something is added into toBox it is selected on.

                  But ok here is what i tried to do, but i kinda got confused in the middle of it.
                  Code:
                        if (document.userForm.assignees.checked == true) {
                            var selected = document.getElementById('toBox')
                            var isSelected = false;
                            for(var i=0; i<=selected.options.length-1; i++){
                                if(selected.options[i].selected ){ 
                                    isSelected = true;
                                    break;
                                }   
                            }
                            if (!isSelected ){
                            alert ("Checkbox 'assignees' must be selected in order to send email to assignees");
                            return false;
                            }
                  	
                        }
                  
                   if (selected.options[i].selected) {
                           var check = document.getElementByID('assignees')
                           var isChecked = false;
                            for(var i=0; i<=check.checked.length-1; i++){
                                if(check.checked){ 
                                    isChecked = true;
                                    break;
                                }   
                            }
                            if (!isChecked ){
                            alert ("Please Select a assignee");
                            return false;
                            }
                  	
                        }
                  Thank you again for all the help (an sorry this is having to be so difficult),
                  Rach

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #24
                    There'd be too much redundancy that way. Try:
                    [CODE=javascript]var isChecked = document.userFo rm.assignees.ch ecked;
                    var selected = document.getEle mentById('toBox ')
                    var isSelected = false;
                    for(var i=0; i<=selected.opt ions.length-1; i++){
                    if(selected.opt ions[i].selected ){
                    isSelected = true;
                    break;
                    }
                    }
                    // now you have isChecked and isSelected set, combine the two
                    if (!isSelected && isChecked){
                    alert ("Checkbox 'assignees' must be selected in order to send email to assignees");
                    return false;
                    }
                    if (!isChecked && isSelected){
                    alert ("Please Select a assignee");
                    return false;
                    }[/code]Do you see how it works?

                    Comment

                    • bonneylake
                      Contributor
                      • Aug 2008
                      • 769

                      #25
                      Hey Acoder,

                      i see how it works/looks a lot better then what i was trying lol. tried for a few hours to get it working but i got nothing. but THANK YOU so much for all the help!

                      But i do got another question. But its more of a just i want to know type question. In the part where it says if(). i see how you do a select an a input. But i am wondering how would i do a checkbox? . here is what i had tried
                      Code:
                        for(var i=0; i<=document.form.check.length; i++){
                                    if(document.form.check[i].checked){ 
                                        isChecked = true;
                                        break;
                                    }  }
                      but i tried all day to just do a check box an i looked online but there so many different ways to do it an none of them that i tried worked. but if you could explain how to do a check box that would be awesome because it was driving me nuts all night.

                      But thank you again for all the help, your awesome!!!

                      Thank you!!!
                      Rach

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #26
                        You made the same mistake of checking for one more than the number of checkboxes. Either remove the =:
                        [CODE=javascript]for(var i=0; i<document.form .check.length; i++){[/CODE]or loop till length-1:
                        [CODE=javascript]for(var i=0; i<=document.for m.check.length-1; i++){[/CODE]

                        Comment

                        Working...