Gibs
Gibs

Reputation: 119

Cancel form submission when entries are invalid

I am working on a form with multiple fields and of course, a submit button. How do I cancel the form submission if some fields are empty? I tried putting a validation of javascript, input type="submit" onclick="check()" on the submit button.

But what I actually want is that, the page won't load so that, all other information in the text fields won't go away. Like, what if the form has 100 fields and the user forgot to input one field and click the submit, it will show him an error message and all other fields will be cleared so he has to type it again. I'm trying to prevent that.

I have multiple options to create this effect but I am currently trying to find a way on doing this. Any other ways would be appreciated (like disabling the button when all mandatory fields are not filled, then enabling it at when everything is complete)

Upvotes: 0

Views: 383

Answers (2)

Anri
Anri

Reputation: 1693

if the condition will be false click(event) returns false and nothing will happend

function check(){

    if(validate === false)
       return false;

}

Enjoy :)

Upvotes: 2

Ben
Ben

Reputation: 1143

You can simply return false from inside your check function to prevent the form from submitting.

Upvotes: 0

Related Questions