Search code examples
phpjqueryformsvalidationdisable-link

Disable submit button when form submit


I am using this validation code, I want to disable submit button when form submit after validation. Submit Button:

<input type="submit" class="submitStandard" value="Save Info" >

Validation code:

<script type='text/javascript'>//<![CDATA[
                $(".submitStandard").on('click', function() {
                    $('form').validate({
                        ignore: [],
                        rules: {
                            required: {
                                required: true
                            },
                            shopname: {
                                required: true
                            }
                        },
                        highlight: function(element) {
                            $(element).closest('.form-group').addClass('has-error');
                        },
                        unhighlight: function(element) {
                            $(element).closest('.form-group').removeClass('has-error');
                        },
                        errorElement: 'span',
                        errorClass: 'help-block',
                        errorPlacement: function(error, element) {
                            if(element.parent('.input-group').length) {
                                error.insertAfter(element.parent());
                            } else {
                                error.insertAfter(element); 
                            }
                        }
                    });
                });//]]>
            </script>

Any best possible solution, submit should be disable when there is not any validation error.

Problem: When I Save info, If I click on submit button twice or three time, It save value twice or three, It should be click once, then it should be disable.


Solution

  • Just add:

    $(this).prop("disabled", true);
    

    Full Code:

    $(".submitStandard").on('click', function() {
      $(this).prop("disabled", true);
      $('form').validate({
        ignore: [],
        rules: {
          required: {
            required: true
          },
          shopname: {
            required: true
          }
        },
        highlight: function(element) {
          $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
          $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
          if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
          } else {
            error.insertAfter(element); 
          }
        }
      });
    });
    

    Or on submit event of the <form>:

    $('form').submit(function(){
        $(this).find(".submitStandard").prop('disabled', true);
    });
    

    Note: I can see that you are using a generic $("form"). This is not recommended since when the JavaScript loads, it blindly applies for all the <form> elements. So it is better to give an id.