1

Is there posisble to prevent user from clicking twice in input tag button type submit? I have tried ondblclick="javascript:void(0)" But its not working

Mine is in a Html.BeginForm not a form element tag

Grateful for all the help!

  • 5
    Please have a look at http://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery – Prakash Thete Aug 02 '16 at 12:13
  • You can hide the button on click event and show similar disable button – rohitr Aug 02 '16 at 12:20
  • hiding is not gonna work if you click fast twice if im right – Hanna Persson Aug 02 '16 at 12:29
  • 1
    if you only try to prevent the click handler to be called twice during a timespan, you can add a custom property to your button, e.g. 'data-lastClick' storing the clickDate and check for currentData-clickDate to cancel the event. Just an idea and not 'pretty', but keeps the logic inside the button. – Wolfgang Aug 02 '16 at 14:01

3 Answers3

1

You can try one click event, it will disable second click event

$( "#XXXXX" ).one( "click", function() {
     alert( "This will click only once." );
});
0

Some Idea

Put something like this to your JavaScript Code:

 var tryNumber = 0;
 $('input[type=submit]').click(function (event) {
     var self = $(this);

     if (self.closest('form').valid()) {
         if (tryNumber > 0) {
             tryNumber++;
             // if you want an alert:
             // alert('Your form has been already submited. wait please');
             return false;
         }
         else {
             tryNumber++;
         }
     };
 });

Another Idea - MVC Action Filter

Create a Custom ActionFilter that will keep track of the Request and add a delay value. A good explanation is on this blog. Of course this need some effort, but would finally need only one Custom Attribute on your Request function.


My 2 Cents

Personally i would say this is not the right way to look at the problem.

You can't prevent your Page from every unwanted User Interaction. Most of the time the user is doing something completed different as expected.

Instead i would make my POST Request stable and overthink my logic to make sure no Data gets corrupted.

There are some Design Patterns like the Post/Redirect/Get Pattern for this.

0

onclicking the submit button

$('.submitButton').prop('disabled',true); 
Parithiban
  • 1,656
  • 11
  • 16