LCJ
LCJ

Reputation: 22662

Validator causes improper behavior for double click check

I have following script to prevent a button from submitting twice by double clicking. It works fine.

But the scenario gets clumsy when I added a Required Field validator. If I try to submit with a blank value in textbox, the validator fires. But when I entered value in textbox and try to submit again it does not do a postback.

I understand the reason, in the first button click itself, the variable isActionInProgress is set as 'Yes' irrespective of the validation error.

What is the best way to overcome this challenge?

MARK UP

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.1.js"></script>
<script type="text/javascript">

    $(document).ready(function () {



        var isActionInProgress = 'No';

        $('.myButton').click(function (e) {

            if (isActionInProgress == 'No') {
                isActionInProgress = 'Yes';
            }
            else {
                e.preventDefault();
                //alert('STOP');
            }

        });


    });

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" CssClass="myButton" runat="server" Text="Submit" ValidationGroup="ButtonClick"
        OnClick="Button1_Click" />

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtEmpName"
        runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="ButtonClick" Text="*"></asp:RequiredFieldValidator>
</div>
</form>
</body>

REFERENCES

  1. Page_ClientValidate is validating multiple times.
  2. Hide redundant error message in ASP.Net ValidationSummary
  3. MSDN - ASP.NET Validation in Depth

Upvotes: 0

Views: 2291

Answers (1)

Boriss Pavlovs
Boriss Pavlovs

Reputation: 1441

I think somthin like this:

 $('.myButton').click(function (e) {
        if(Page_IsValid){
        if (isActionInProgress == 'No') {
            isActionInProgress = 'Yes';
        }
        else {
            e.preventDefault();
            //alert('STOP');
        }
      }

    });

Upvotes: 2

Related Questions