javascript if condition && condition doesn't work
28,377
Solution 1
It sounds like ||
is what you are looking for here. The &&
operator is only true if both the left and right side of the &&
are true. In this case you appear to want to display the message if the value is 1
or empty. This is exactly what the ||
operator is for. It is true if either the left or right is true
Solution 2
If Or operator is working, means there are some javascript errors in your second part of condition. check document.form.des.value=='' (maybe just open your javascript console in Chrome/Firefox/IE8+)
Related videos on Youtube
Comments
-
Ivo San almost 4 years
the AND && operator doesn't work, but when i replace it with an OR || operation it is workin, why? I just used OR || operator for testing, what i need is an && operator. Please help. thanks
function validate() { if ((document.form.option.value == 1) && (document.form.des.value == '')) { alert("Please complete the form!"); return false } else return true; }
i also tried nested if but it doesn't work too
if(document.form.option.value==1) { if(document.form.des.value=='') { alert ("Please complete the form!"); return false } }
-
PoeHaH almost 12 yearsso it must be a logical error
-
starbeamrainbowlabs almost 12 yearsTo make sure that the user completes a form, try using html5 form validation.
-
Ivo San almost 12 yearsthanks @starbeamrainbowlabs, html5 vadidation is so easy. NICE
-
-
Ivo San almost 12 yearsim going to test both left and right, both must must be true. i need &&