Reputation: 841
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
Reputation: 5308
The ordering of your conditions are incorrect.
As it stands:
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
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
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
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