tags:

views:

159

answers:

5

I have a JavaScript method that i want to validate a form

if the validation fails so

if (validationChecks ...... ){
    return alert("message")
}
else{
//proceed
}

however no matter what i do in the if the form still seems to submit, any ideas?

+2  A: 

you should return false after checking validation.


if (validationChecks ...... ){ 
     alert("message") 
    return false; } 
else{ 
//procced 
} 
valli
thanks for the help
tommy
@stan: accept the answer if it solves your problem
Jan Hančič
A: 

You need to return false; in the case of failure

Tim
+4  A: 

You have spelt 'return' wrong. Also, returning an alert will not work, you'll need to return false explicitly to stop the form submitting.

if (validationChecks ...... ){ 
    alert("message");
    return false; 
} 
else{ 
//procced 
}
Mark B
+4  A: 

Your validation must return false when the form is not valid, and if you're calling your validationin onSubmit, then you must make sure that you include a return statement there as well:

<form onSubmit="return validationChecks();">
David Hedlund
A: 

I think "return" is better than "reutrn". An error is thrown, but you don't see it as the page is reloaded.

Fabien Ménager