Sunday, October 20, 2013

How to Prevent Page Postback if Validation is false using Jquery in ASP.Net

Leave a Comment
Let us check in this tutorial how to prevent server side code from executing  if the validation is false using jquery in
Some time you want to prevent the code from running if user haven't filled certain required fields.
In this example I am using simple text box control and if user haven't filled the text box then the button click event won't execute on the server side.

Sample code :
Explanation: The Button1 Click event won't be fired until user doesn't enter some text in the text box. So in this way you can prevent server code from executing until validation are not satisfied using jquery.

<html xmlns="">
<head runat="server">
    <script src="//"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $('#Button1').click(function () {

                var uname = $('#txtname').val();
                if (uname == '') {
                    alert("Please Enter Name");
                    return false;
                    return true;


    <form id="form1" runat="server">
            <asp:TextBox runat="server" ID="txtname"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>


Code Behind Button Click Event:
 protected void Button1_Click(object sender, EventArgs e)



Post a Comment