4

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?

1

4 Answers 4

9

you should return false after checking validation.


if (validationChecks ...... ){ 
     alert("message") 
    return false; } 
else{ 
//procced 
} 
Sign up to request clarification or add additional context in comments.

Comments

5

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 
} 

Comments

5

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();">

Comments

1

You need to return false; in the case of failure

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.