When you say it's not working, what's happening instead?
Multiple select box, pop up question
Collapse
X
-
Originally posted by acoderWhen you say it's not working, what's happening instead?
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; }
Thank you,
RachComment
-
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; }
Thank you,
RachComment
-
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; } }
Comment
-
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 :),
RachComment
-
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
-
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; } }
RachComment
-
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
-
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 thank you again for all the help, your awesome!!!
Thank you!!!
RachComment
-
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
Comment