0

I am trying to add a barcode verify function to an item picking webapp page and i have the javascript as this:

 function barcodeSubmit () {

 if (barcode1 != barcode || barcode2 != barcode) {
   if (PUT_LPN == "") {
  barcodeF.focus();
  return false;
  }  
 } else {
 if (barcode1 != barcode || barcode2 != barcode) {
 if (PUT_LPN != "") {
     barcodeF.focus();
     return false;
     }  
   }
 }
} else {
 if (barcode1 == barcode || barcode2 == barcode) {
 if (PUT_LPN == "") {
    PUT_LPN.focus();
    return false;
  }  
 }
}
 else {
  if (barcode1 == barcode || barcode2 == barcode) {
  if (PUT_LPN != "") {
    return true;
   }  
  }
 }
}

When I submit this form it does not submit and I can not get it to do the function its connected to an onclick input field . It does call when I make it simpler it will execute what I want. I also tried it without the else statements and came out with slightly different result but still not triggering event properly

3
  • "When I submit this form i ", here this refer to which form Commented Sep 8, 2015 at 5:00
  • You have one if with 4 else which is wrong. Use else if Commented Sep 8, 2015 at 5:00
  • If (){}else if(){} else if(){} else{} Commented Sep 8, 2015 at 5:01

4 Answers 4

1

Use switch instead:

switch(true) {
    case ((barcode1 != barcode || barcode2 != barcode) && PUT_LPN == ""):
        barcodeF.focus();
        return false;
    case ((barcode1 != barcode || barcode2 != barcode) && PUT_LPN != ""):
        barcodeF.focus();
        return false;
    case ((barcode1 == barcode || barcode2 == barcode) && PUT_LPN == ""):
        PUT_LPN.focus();
        return false;
    case ((barcode1 == barcode || barcode2 == barcode) && PUT_LPN != ""):
        return true;
    default:
        return true;
}

You can remove default case if not needed

Sign up to request clarification or add additional context in comments.

Comments

1

Your code having lot of problems you are using one if and 4 else and some unused conditions also try following:

function barcodeSubmit() {

    if (barcode1 != barcode || barcode2 != barcode) {
        if (PUT_LPN == "") {
            barcodeF.focus();
            return false;
        } else if (PUT_LPN != "") {
            barcodeF.focus();
            return false;
        }
    }
    else if (barcode1 == barcode || barcode2 == barcode) {
        if (PUT_LPN == "") {
            PUT_LPN.focus();
            return false;
        } else if (PUT_LPN != "") {
            return true;
        }
    }
}

Comments

1

Please try something like following

function barcodeSubmit () {

 if (barcode1 != barcode || barcode2 != barcode) {
	if (PUT_LPN == "") {
	  barcodeF.focus();
	  return false;
	}  
 } else if (barcode1 != barcode || barcode2 != barcode) {
	if (PUT_LPN != "") {
	  barcodeF.focus();
      return false;
    }   
} else if (barcode1 == barcode || barcode2 == barcode) {
	if (PUT_LPN == "") {
		PUT_LPN.focus();
		return false;
	}  
}else {
	if (barcode1 == barcode || barcode2 == barcode) {
		if (PUT_LPN != "") {
			return true;
		}  
	}
}

}

Comments

1

Problem seems to me is PUT_LPN.focus() and PUT_LPN == "". I assume its and input field and in order to compare value of it you need to use PUT_LPN.value. Again the syntax of if...else is also not valid.

if(condition) {
  //
} else if(condition) {
  //
} else {
  //
}

Also there are lot many repetitive conditions which you need to take care of.

function barcodeSubmit() {
  if (barcode1 === barcode || barcode2 === barcode) {
    var flag = ('' === PUT_LPN.value);
    if (flag)
      PUT_LPN.focus();
    return !flag;
  }
  barcodeF.focus();
  return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.