I'm trying to stop the js execution if the validate function returns false. However, if it's false, the script doesn't stop and I can se the message "This will run??" - Would someone give me a hint as to why calling the validate() function inside submit() doesn't work? What's wrong here? Thanks in advance.
// validate name
function validate(name){
if(name.length < 1){
console.log('false - length: ' + name.length);
return false;
}
}
// submit function
function submit(){
var name = '';
// if name is empty, the script should stop.
validate(name);
console.log("This will run??");
return true;
}
submit();
returnonly applies to the function body it appears in.if (!validate(name)) return false;And you also need toreturn trueat the end ofvalidatewhen the check passes.