I have written a form validation using JS which ends with return(true);
function check() {
....validation code
return(true);
}
All I want i
First of all, return
is not a function, you can just do this:
return true;
Now, to only execute myFunction
if check
returns true
, you can do this:
check() && myFunction()
This is shorthand for:
if(check()){
myFunction();
}
You don't need to compare the return value of check
with true
. It's already an boolean.
Now, instead of myFunction()
, you can have any JavaScript code in that if
statement. If you actually want to use, for example, myFunction
, you have to make sure you've defined it somewhere, first:
function myFunction() {
// Do stuff...
}