Bato Dor
Bato Dor

Reputation: 841

if and or clauses in javascript

i cant understand why this simple function doesnt work in the way i want it to work :)

c = document.form.product_catid.selectedIndex;
if (document.form.product_name.value == "" && document.form.product_catid[c].value == "")
{
    alert ("Please, define at least product name or product category !");
    return false;
}
else if (!(document.form.product_name.value == "")) 
{
    return true;
}
else if (!(document.form.product_catid[c].value == "")) 
{
    return true;
}
else
{ 
    alert ("Please, dont define two of them at the same time!");
    return false; 
}
return true;

all i want is when input with name product_name is filled OR select with name product_catid is selected, the function is returned true, but if none of them or both are defined, i want it to alert two different alerts ) thank you all for the help, i really appreciate it!

Upvotes: 0

Views: 286

Answers (4)

WickyNilliams
WickyNilliams

Reputation: 5308

The ordering of your conditions are incorrect.

As it stands:

  1. Your first check is that both to handle the scenario where both inputs are empty, this works as expected.
  2. Your next check is that one of the values isn't empty
  3. the next check is that the other value isn't empty
  4. then you assume that by this point both inputs aren't empty (your else condition)

Consider this: condition 1 ensures that both inputs aren't empty - which means, logically, at least one of them is non-empty. Following on from this, given that we now know at least one of them is non-empty, condition 2 or condition 3 will always evaluate to true. The result of this is that the 4th code path will never execute.

To remedy this, put your check that both inputs are not populated as an explicit condition.:

function validateInput() {

    //replace values here to test function
    var productCatId = "",
        productName = "gjghj";

    //uncomment when using real values
    //var productCatId = document.form.product_catid[document.form.product_catid.selectedIndex].value,
    //    productName = document.form.product_name.value;

    //check both aren't empty
    if (productName == "" && productCatId == "")
    {
        alert ("Please, define at least product name or product category !");
        return false;
    }
    //check both aren't populated
    else if (productName != "" && productCatId != "")
    { 
        alert ("Please, dont define two of them at the same time!");
        return false; 
    }
    //by now we know that both aren't empty, and both aren't populated, therefore only one is populated, so return true;
    return true;

}

Upvotes: 0

gdoron
gdoron

Reputation: 150313

Once you use the return statement the function "ends":

alert ("Please, define at least product name or product category !");
return false; // Exit point
...
...
// this will not be executed.
alert ("Please, dont define two of them at the same time!"); 
return false;

var returnValue = true;

if (document.form.product_name.value == "" && document.form.product_catid[c].value == "")
{
    alert ("Please, define at least product name or product category !");
    returnValue = false;
}
else if (document.form.product_name.value == "") 
{
    return true;
}
else if (document.form.product_catid[c].value == "") 
{
    return true;
}
else
{ 
    alert ("Please, dont define two of them at the same time!");
    returnValue = false; 
}
return returnValue;​

Upvotes: 1

CodingHamster
CodingHamster

Reputation: 362

Try this:

var product = document.form.product_name.value;
var category = document.form.product_catid[document.form.product_catid.selectedIndex].value
if (product || category) {
    if (product && category) {
        alert ("Please, dont define two of them at the same time!");
        return false;
    }
    else return true;
}
else {
    alert ("Please, define at least product name or product category !");
    return false;
}

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32306

Try debugging your case using Chrome Developer Tools, Firebug, or something similar. My suspicion is that you are trying to compare "" and undefined in the document.form.product_catid[c].value == "" clause (this will be the case when selectedIndex is -1). Try just !document.form.product_catid[c].value instead.

Upvotes: 2

Related Questions