JavaScript code to stop form submission

前端 未结 12 2085
感情败类
感情败类 2020-11-21 06:40

One way to stop form submission is to return false from your JavaScript function.

When the submit button is clicked, a validation function is called. I have a case i

12条回答
  •  忘了有多久
    2020-11-21 06:59

    Hemant and Vikram's answers didn't quite work for me outright in Chrome. The event.preventDefault(); script prevented the the page from submitting regardless of passing or failing the validation. Instead, I had to move the event.preventDefault(); into the if statement as follows:

        if(check if your conditions are not satisfying) 
        { 
        event.preventDefault();
        alert("validation failed false");
        returnToPreviousPage();
        return false;
        }
        alert("validations passed");
        return true;
        }
    

    Thanks to Hemant and Vikram for putting me on the right track.

提交回复
热议问题