Link to home
Start Free TrialLog in
Avatar of snocross
snocross

asked on

Halting execution of Javascript code

Is there a way that I can halt the execution of my Javascript code if a certain condition is met? I call a series of functions, one after another, and if one function returns a certain value I do not want to continue with the next function. I realize I could probably set a variable and check this variable in each function but it seems like a lot of code. Is there some sort of a STOP or END or EXIT command?
Avatar of ghassan99
ghassan99

Just use 'Return' to exit the function.

-Gus

Avatar of snocross

ASKER

Gus, I don't think that will work for my situation becuase I also want to stop executing ALL of the javascript, not just exit the one function.  For example if I call several functions when I press a SUBMIT button then if the first function has a problem I don't want the second function to evaluate.  

Example:

function saveSubmit() {
brokerValidate();
customerValidate();
otherValidate(); ....etc }

function brokerValidate() {if (document.forms[0].Broker.value == "") {alert("Please enter a broker number.");
THIS IS WHERE I WANT TO STOP, OTHERWISE THEY GET PROMPTED FOR EVERY FIELD THEY LEFT BLANK, ALL AT ONCE!}      

I realize I could put all the validation in one function and use IF, ELSE IF,  but the problem is not only do I check the validation when the form is submitted but also each time they tab out of a field so I need each validation to be a separate function.


Gus, I don't think that will work for my situation becuase I also want to stop executing ALL of the javascript, not just exit the one function.  For example if I call several functions when I press a SUBMIT button then if the first function has a problem I don't want the second function to evaluate.  

Example:

function saveSubmit() {
brokerValidate();
customerValidate();
otherValidate(); ....etc }

function brokerValidate() {if (document.forms[0].Broker.value == "") {alert("Please enter a broker number.");
THIS IS WHERE I WANT TO STOP, OTHERWISE THEY GET PROMPTED FOR EVERY FIELD THEY LEFT BLANK, ALL AT ONCE!}      

I realize I could put all the validation in one function and use IF, ELSE IF,  but the problem is not only do I check the validation when the form is submitted but also each time they tab out of a field so I need each validation to be a separate function.


ASKER CERTIFIED SOLUTION
Avatar of bennyliaw
bennyliaw
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bennyliaw,

Correct me if I'm wrong but it looks like your example would process all of the functions until it found the first error.  This is great but I also call the functions one at a time.  If I'm tabbing out of the CUSTOMER field I just want to validate the CUSTOMER field... I don't want to check every field at this point, not until I press the SUBMIT button at which point I think your code would work great.  Basicly I need to do two things, first validate each field one function at a time as I exit each field, and secondly I need to validate the entire form (all validation functions) when the form is submitted.
Then why dont u just put the function call of each field in the onBlur event of that field?  Or is there some fields that rely on the values of other fields?

-Gus
Sorry disregard my last comment as I didnt realize that u added a comment to Benny's comment.

-Gus
Snocross,

Sorry for the late respond, I did not see your reply to my comment when I submitted, probably because of the slow internet access here.

I believe Gus' way is the only way to validate the field when the user tab out of the field.

benny
I didn't quite understand your syntax (ie: if F2(){Return code} ) but I ended up setting a flag like you said and it worked.