0

When I run this code it doesn't seem to be doing what I expect. So this is a validation, and when I run the if statement, the validator stops running when it hits the false. But when it hits the false from the other function numCheck, it alerts but then just continues. Since there are more validations below, if the numCheck comes back clean, how do I continue through the rest of the validation rather than cutting out if it's true? Where am I going wrong?

function numCheck(num){
	if(isNaN(num)){
		alert("Vendor number must be numeric");
		focusTo(document.forms["parts"]["vendorNo"]);
		return false;
	}
}

function validateForm(){
//DECLARATION
	var vendorNo = document.forms["parts"]["vendorNo"].value;
//VENDOR NUMBER
  numCheck(vendorNo);
	if(vendorNo.length != 4){
		alert("Please enter 4 digit Vendor Number");
		focusTo(document.forms["parts"]["vendorNo"]);
		return false;
	}

Thank you in advance, I've spent a lot of time trying to figure this out.

2 Answers 2

1

You must also return from the parent (calling) function, validateForm

Your subroutine numCheck will return back to the calling function validateForm

function numCheck(num){
	if(isNaN(num)){
		alert("Vendor number must be numeric");
		focusTo(document.forms["parts"]["vendorNo"]);
		return false;
	}
return true;
}

function validateForm(){
//DECLARATION
	var vendorNo = document.forms["parts"]["vendorNo"].value;
//VENDOR NUMBER
  if (numCheck(vendorNo)==false){
     console.log("Invalid vendorNumber");
     return; // you must also return from the calling function, otherwise the code will still continue.
  }
	if(vendorNo.length != 4){
		alert("Please enter 4 digit Vendor Number");
		focusTo(document.forms["parts"]["vendorNo"]);
		return false;
	}

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

Comments

1

returning from numCheck does not mean validateForm has returned anything. Check the return value of numCheck and then take the appropriate action.

function validateForm(){
  ...
  // numCheck returns a boolean here. If it returns false, return from this function
  if(!numCheck(vendorNo)) {
    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.