---
title: "Run javascript first and on success run code behind"  
description: "Run javascript first and on success run code behind"  
author: "Anonymous User"  
published: 2014-11-27  
updated: 2014-11-27  
canonical: https://www.mindstick.com/forum/12707/run-javascript-first-and-on-success-run-code-behind  
category: "asp.net"  
tags: ["asp.net", "javascript"]  
reading_time: 2 minutes  

---

# Run javascript first and on success run code behind

In Asp page i have below controls. A input [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) ,[Imagebutton](https://www.mindstick.com/articles/12753/imagebutton-in-android) and a [label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better).

```
<input id="txtTotamt"  runat="server" type="text" value="0" /><asp:ImageButton ID="Validate" runat="server"  OnClientClick="return validatecontrol();"/><asp:Label ID="lblerror" runat="server"></asp:Label>
```

And in [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) in page

```
<script language="javascript" type="text/javascript">    function validatecontrol()     {        var valid_amt = document.getElementById("txtTotamt").value;        if isNaN(valid_amt) == false {            if(valid_amt % 1 != 0) && (valid_amt>0){                return true;            }else{               document.getElementById("lblerror").innerHTML ="Error";            }        }else{        document.getElementById("lblerror").innerHTML ="Error";        }    }</script>
```

## In [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind)

Protected Sub Validate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles [Validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value).Click

//my codes go here

End Sub

I want to validate the [content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand) of textbox in a JavaScript and I also have code behind for that [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click).I want scrip to be executed first and if the text in input text not proper then code behind should not execute. But it does not work for me. I think the code behind in .net gets triggered before the JavaScript. How it can be solved? Is there any [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) in my javascript?

## Replies

### Reply by Pravesh Singh

Hi Ankita, \

Please modify your script as:

```
 <script type="text/javascript">        function validatecontrol()         {            var valid_amt = document.getElementById("txtTotamt");             if(isNaN(valid_amt) == false)            {               
     if((valid_amt % 1 != 0) && (valid_amt>0))                {                   
            return true;                }            else                {                   
document.getElementById("lblerror").innerHTML="Error"; return false;            }        }         else        {       
document.getElementById("lblerror").innerHTML="Error"; return false;        }        }</script>
```


---

Original Source: https://www.mindstick.com/forum/12707/run-javascript-first-and-on-success-run-code-behind

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
