Pomster
Pomster

Reputation: 15197

how to stop form submit/postback with Javascript?

I have a button to submit my form with a postback, i would like to run some valuation and if all is not well show error messages and stop the postback. I have managed to get my code to enter my onsubmit function but am unable to stop the postback.

Form:

<form id="form1" runat="server" style="width:100%; height:400px; margin:0px; padding:2px;" class="BackColor5" enableviewstate="true" onsubmit="validateForms()">

Function:

function validateForms() {
   if ($('#ddAssets').text() == "") {
      $('#ddAssets').css({border: "1px solid red"});
      return false;
   }
}

Button:

<ICCM:ICCMImageButton ID="btnSubmit" runat="server" onclick="btnSubmit_Click" TabIndex="6" meta:resourcekey="btnResSubmit" PostBack="true"  style="display:inline-table;" AccessKey="S"/>

btnSubmit_Click is a function in the code behind.

Upvotes: 2

Views: 4974

Answers (1)

Adil
Adil

Reputation: 148110

Return the result of validateForms() in onsubmit

onsubmit="return validateForms()"

Return true when validation succeed and false otherwise.

function validateForms() {
   if ($('#ddAssets').text() == "") {
      $('#ddAssets').css({border: "1px solid red"});
      return false;
   }
   return true;
}

Upvotes: 7

Related Questions