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.

Sabitha
  • 273
  • 4
  • 14

2 Answers2

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

Community
  • 1
  • 1
codemirror
  • 3,164
  • 29
  • 42
0
var isallpassed=0;    
function getFormData(form)  
    {  
        isallpassed=validation1 failed;
        isallpassed= isallpassed && validation2 failed;
        if(isallpassed)  
        {  
         return $(form).serialize(); 
        }  
      else{
        alert("Invalid input");
       }
    }
Anil
  • 3,722
  • 2
  • 24
  • 49