How to prevent postback using javascript

Actually this code works well in firfox mozila, but it does not work 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 answerlength == 0 ; when answerlength == 0 ; but when answer length ==0 , then it warns an alert("u can't submit blank answer") message and postback on the io server wants to prevent it, how do I do it?

+4
source share
2 answers

It does not work, 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> 
+2
source

try it

 OnClientClick="doSubmit(this); return false;" 
+1
source

All Articles