0

I have a javascript method for validating the inputs in form and then return the serialised form data if it has passed validation.

function getFormData(form)  
{  
  if(validation failed)  
  {  
    alert("Invalid input"); 
    return;   
    //stop execution;  
  }  
  return $(form).serialize();  
}  

I have another function calling the above function.

var data = getFormData();

The problem I face is, if validation failed, the code execution doesn't stop execution. How to stop execution after alert ? I tried return false; and then having a if check to see if it has returned false, it works. But is there any other way to stop executing immediately after alert. Because, I need to call this function at multiple places.

3
  • if its a event do preventDefault; return false is fine Commented Mar 21, 2017 at 4:57
  • 1
    Possible duplicate of Javascript stop execution abort or exit Commented Mar 21, 2017 at 4:58
  • consider using "else" Commented Mar 21, 2017 at 4:58

2 Answers 2

1

Try this

throw new Error('Generated error manually to stop execution of script');

Or link to more sophisticated answer

How to terminate the script in Javascript

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

Comments

0
var isallpassed=0;    
function getFormData(form)  
    {  
        isallpassed=validation1 failed;
        isallpassed= isallpassed && validation2 failed;
        if(isallpassed)  
        {  
         return $(form).serialize(); 
        }  
      else{
        alert("Invalid input");
       }
    }

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.