Click to See Complete Forum and Search --> : Breaking out of a function
kelemvor
03-12-2003, 09:50 AM
I have a form and when certain data is entered, it needs to validate that the number is an OK number, and then does some totaling. I want it to validate the number but then break outof the funtion if it's invalid and not do the totaling.
How do I break out? Is there just a Break or EndFunction command or something I can put in there?
Thanks.
gil davis
03-12-2003, 10:00 AM
The "break" statement can be used to terminate a "switch", "loop" or "label" statement. However, form validation routines usually require a boolean value to be returned to the onsubmit event to control the form submission. You can use "return false" to terminate a function and stop the submission anywhere in the validation function, as many times as you like (or need).
kelemvor
03-12-2003, 10:02 AM
I'm not doing this check onSubmit, I'm doing it as data is entered to check it realtime. So I'm looking for a way that I can stop a function right in the middle of it and make sure the user fixes the data entered before theyu can continue.
Not sure if it's possible or not but I assume there's some way to just totally stop processing somehow.
Just use a variable to let the page know if it should continue on. Something like this...
Set up a variable at the very top of your script...
var isValid = "yes";
then, where it validates the number, set it to, isValid = "no"; if it is an invalid number. Now, add the rest of your form checking inside of an if command like this:
if (isValid == "yes")
{
// continue validating..
}
else
{
// don't validate any more...
}
kelemvor
03-12-2003, 10:37 AM
Yeah I thought about putting all the validation inside it's own IF Statements so if the first part fails it just doesn't go to the TRUE part of the IF statement but I thought maybe there was a command to just bail out of the function right in the middle.
I guess I'll just use IF statements since it's probably just as easy.
Thanks.