简体   繁体   中英

Prevent form submission when validation fails on submit button click

On submit button click I need to check the validation if its returns true . I need to submit the form. But when it returns false also the form gets submitted and the event SaveProjects() gets called. How can I prevent the form submission when CheckValidation() returns false ?

 <script type="text/javascript> 
   function CheckValidation()
      {
       alert();
      return false;
      }

</script>
    @using (@Ajax.BeginForm("SaveProjects", "Projects", null, new AjaxOptions { OnSuccess =     "OnSuccessProject", HttpMethod = "post" }, new { id = "ProjectsForm" }))
       {     <div class="outerBox" id="divProjectCreate">

      @Html.Partial("../Projects/_CreateOrEdit", Model)
         <div class="row centerAlign">
          <input type="submit" value="Save" id="btnSaveProjects"  onclick="CheckValidation()"/>
         <input type="button" value="Cancel" id="btnCancel" />
         </div>

        </div>

}

you can do

   $('#target').submit(function() {
      alert('Handler for .submit() called.');
      return CheckValidation();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM