javascript - What is the proper format of this if/else statement? -


function checkforinput() {      var valid = true;     var date = document.getelementbyid('date');     var time = document.getelementbyid('time');     var checkbox = document.getelementbyid('checkbox-1-1');     if (date.value == "" && time.value == "" && checkbox.checked == false || date.value !== "undefined" && time.value !== "undefined" && checkbox.checked) {         document.getelementbyid('errmsg10').innerhtml = "*please schedule in advance or select now";         valid = false;     } else {         document.getelementbyid('errmsg10').innerhtml = "";     }      return valid; } 

the html:

<input id="date" class="datentime" type="date" name="date" min="2015-07-18" max="2015-12-31" value="1"/> <input id="time" class="datentime" type="time" name="time" value="1"/><br> <h2 class="bottomopt">or</h2><span id="errmsg10"></span> <h1 class="bottomopt"><span class="adnow">now</span><br><span id="asap">*we'll send asap.</span></h1> <input type="hidden" name="now" value="0"/> <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" name="now" value="1" /><label for="checkbox-1-1"></label> <input type="hidden" name="status" value="3"/> 

in above code, trying require input date, time , checkbox cannot empty, cannot all selected either. it's either user checks checkbox or selects date , time.

the first part of if/else statement seems working fine, code after || in conditional, seems wrong. if date , time selected checkbox unchecked, code works. if checkbox checked, date , time not selected, code doesn't work. still displays errmsg10.

if ( (!date.value && !time.value && !checkbox.checked)    || (date.value  && time.value && checkbox.checked)) { 

because condition or conditional, group 2 statements explicit blocks the correct value evaluated.

also should fine check !time.value instead of checking undefined explicitly. take care checking undefined, null or 0.

also note using === faster operation using ==


Comments