1

I am creating registration page and doing null field validation on submit button click using jquery. if there is any error in form validation then i am preventing default method call using jquery, so it will not call code behind button click event.

Problem: sometimes user double clicked on button and this is calling code behind button click event two times with two database row insertion having a same data.

I tried lots of solution but unfortunately i couldn't make any success. Please help me to solve out this problem if you have any solution.

Thanks in advance,

3
  • In client side, set the button disabled when clicked, and enable it after ajax complete event Commented Jun 27, 2013 at 10:39
  • i am using jquery for only null field validation. actually it is asp:button and data insertion is performed inside server side button click event. Commented Jun 27, 2013 at 12:02
  • If you calling Jquery function on button click, its good habbit later call ajax function ie ur [WebMethod] function Commented Jun 27, 2013 at 12:50

6 Answers 6

2

Actually, i was preventing default server side method call in jquery when button is clicked using e.PreventDefault() method of jquery if validation is false. Don't know what was the problem but when i set function on client click of button and return true/false based on validation instead of e.PreventDefault, trick worked great for me.

Thanks to all who comment on this question.

Sign up to request clarification or add additional context in comments.

Comments

0

Simply add a variable called 'loading' for example and check if the ajax call is busy:

At the top of your code:

var loading = false;

Then on form submit:

$('#form').submit() {
    if(loading)
        return false;

    loading = true;

    //Ajax call
    $.ajax({
    type: "POST",
    url: "somePage.php",
    data: $('#form').serialize(),
    success: function(response) {
        loading = false;
            //Your response code
    }
    });
}

Comments

0

Use one on the client side. This will prevent double clicks.

$("#button").one("click", function() {
      // validate the form.

      // if form is valid, submit form
});

An alternative solution is to have a boolean flag.

var submitting = false;

   $("#button").click(function() {        
          if (!submitting) {
             submitting = true;

             if (formIsValid) {
                 submitting = false;
                 $("#form").submit();
             } else {
                 submitting = false;
              }
          }
   });

Comments

0

Add disabled attribute to your button as the first thing in your js method.

function func(event) {

$("#button").prop("disabled", true);

..... }

Comments

0

try this it might help for your asp button

<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" UseSubmitBehavior="false" OnClientClick="ValidationCode(event); return false;" />

    <script>
        function ValidationCode()
        {
            //Code of validtion
            event.preventDefault();
            if(true)
            {
                __dopostback:("btnSubmit","");
            }
        }
    </script>

1 Comment

yes it is working ok for first time. it is adding _doPostBack() in button onclick attribute. so when i click second time on same button after first successful insertion, it is not validating my form controls as it is in first call. checking null ,email, phone number validation etc. in jquery function that i am calling when button is clicked.
0

Sample code

Client Side:

   $("#submit").click(function () {

          if (!YourvalidationFunction) 
              {
                    return false;
              }
           else {
                  //ajax_function is a folder contain Default.asmx page and insertNewRecord is function name (server side)
                   $.ajax({
                   type: "POST",
                   url: "ajax_function/Default.asmx/insertNewRecord",
                   data: "{ 'prefix': '" + dataString + "'}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: OnSuccessinsertingwcCall,
                   error: OnErrorCall
                    });
                 }
            });

Server Side:

[WebMethod]
public string[] insertNewRecord(string prefix)
{
         string status = "";
         List<string> d = new List<string>();
         try
         {
           // logic code for insertion if success set msg
             status = "New record inserted";
         }
         catch (Exception ac)
         {
             status = " Error occurs";
         }
         d.Add(string.Format("{0}", status));
         return d.ToArray();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.