Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent postback by javascript

actually this code is working well in firefox mozilla but it's not working in IE8

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnPrimary" OnClientClick="return doSubmit('this');"
        OnClick="btnSubmit_Click" />
      <script type="text/javascript">
           function doSubmit() 
           {
            var ansLength = oDOM.body.innerText.trim().length;
            if (ansLength == 0 && smielyPresent == -1) 
              {
              alert("you cannot submit blank answer");
              return false;
              }
           }
    </script>

     protected void btnSubmit_Click(object sender, EventArgs e)
    {
      // i am doing some stuff
    }

here i want to prevent postback when answerlength == 0 ; but when answer length ==0 then it alert alert("u can't submit blank answer") message and postback to server io want to prevent it how i do ?

like image 953
Nishant Kumar Avatar asked Oct 28 '10 10:10

Nishant Kumar


People also ask

What is postback in JavaScript?

Postback is a mechanism where the page contents are posted to the server due to an occurrence of an event in a page control. For example, a server button click or a Selected Index changed event when AutoPostBack value is set to true.


2 Answers

It's not working since you have a script error in your javascript.

<script type="text/javascript">
    function doSubmit() 
    {
        //I've removed the first equal sign
        var ansLength = oDOM.body.innerText.trim().length; 
        if (ansLength == 0 && smielyPresent == -1) //typo on smielyPresent ?
        {
            alert("u can't submit blank answer")
            return false;
        }
    }
</script>
like image 162
jgauffin Avatar answered Sep 22 '22 22:09

jgauffin


try this

OnClientClick="doSubmit(this); return false;"
like image 45
shashi Avatar answered Sep 22 '22 22:09

shashi