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+)

Share:
28,377

Related videos on Youtube

Ivo San
Author by

Ivo San

samsung galaxy pocket

Updated on July 12, 2020

Comments

  • Ivo San
    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
      PoeHaH almost 12 years
      so it must be a logical error
    • starbeamrainbowlabs
      starbeamrainbowlabs almost 12 years
      To make sure that the user completes a form, try using html5 form validation.
    • Ivo San
      Ivo San almost 12 years
      thanks @starbeamrainbowlabs, html5 vadidation is so easy. NICE
  • Ivo San
    Ivo San almost 12 years
    im going to test both left and right, both must must be true. i need &&