Home > DeveloperSection > Forums > Run javascript first and on success run code behind
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    November-27-2014 1:05 AM

 ASP.Net ASP.Net  JavaScript 
Ratings:


 1 Reply(s)
 814  View(s)
Rate this:

In Asp page i have below controls. A input textbox ,Imagebutton and a label.

 

<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 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

 

Protected Sub Validate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Validate.Click

//my codes go here

End Sub

I want to validate the content of textbox in a JavaScript and I also have code behind for that 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 in my javascript?



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-27-2014 1:55 AM

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>


Don't want to miss updates? Please click the below button!

Follow MindStick