JavaScript jQuery validate submits form twice jquery validation submit invalid form,jquery validate form still submits

I'm using jQuery validator and jQuery 1.8.3. For some reason my form is submitted twice which causes errors. This is the code:

someNameSpace.myValidateFunction = function(){
    $('#myForm').validate({
        submitHandler: function(){
            mySubmitCallBackFunction();
        },
    });
    return false;
};

someNameSpace.myValidateFunction();
$(document).on('click', '#myFormSubmitButton', function(){
    $('#myForm').submit();
});

Any idea why the form is submitting twice? The form button is not an input submit but a <button> element (needed in this case).

Answer:1

Your form is submitted twice because :

  • you call $('#myForm').submit()
  • you click on the submit button which also triggers the form event. Note : the , if it is in a form, will also trigger the form event on click

So I think you have to add a return false in your on click method to prevent the form to be submitted when you click on the submit button. Now, only $('#myForm').submit(); will submit the form :

someNameSpace.myValidateFunction = function(){
    $('#myForm').validate({
        submitHandler: function(){
            mySubmitCallBackFunction();
        },
    });
    return false;
};

someNameSpace.myValidateFunction();
$(document).on('click', '#myFormSubmitButton', function(){
    $('#myForm').submit();
    return false;
});

I think you also don't need to add this :

$(document).on('click', '#myFormSubmitButton', function(){
    $('#myForm').submit();
    return false;
});

The validate method will be automatically called when the form is submited.

Answer:2

You don't need to add following code

$(document).on('click', '#myFormSubmitButton', function(){
  $('#myForm').submit();
});

submitHandler code will automatically handles form submit.

Answer:3

I had the same issue: my goal: validate many forms with the same class:

my php:

form class="js-validate-itemForm" method="post" action="">
...
form class="js-validate-itemForm" method="post" action="">

my js:

$('.js-validate-itemForm').each(function () {
       $(this).validate({
           submitHandler: function (form, event) { 
               doActionOnItemform(form, event);
               return false;
           }
        });
    });
Answer:4

Our applications loading process is lengthy, so to provide the user with indication the app is functioning and merely loading, we want to provide a loading/initializing indicator. I tried using WL's ...

Our applications loading process is lengthy, so to provide the user with indication the app is functioning and merely loading, we want to provide a loading/initializing indicator. I tried using WL's ...

I have the following simple example, When the line extends outside the rectangle, I want to clip it. I already have the rectangle used as an outline, what is a simple way to the same rectangle as a ...

I have the following simple example, When the line extends outside the rectangle, I want to clip it. I already have the rectangle used as an outline, what is a simple way to the same rectangle as a ...

I'm trying to learn Knockout and I'm following these two tutorials: Tutorial 1 Better list example But after half a day of trying (and failing), I'm not able to add or remove an item. Here is my ...

I'm trying to learn Knockout and I'm following these two tutorials: Tutorial 1 Better list example But after half a day of trying (and failing), I'm not able to add or remove an item. Here is my ...

  1. remove items from list python
  2. remove items from array javascript
  3. remove items from list
  4. remove items from amazon history
  5. remove items from startup windows 10
  6. remove items from apple wallet
  7. remove items from launchpad
  8. remove items from dictionary python
  9. remove items from icloud
  10. remove items from kindle library
  11. remove items from list c#
  12. remove items from etsy cart
  13. remove items from menu bar mac
  14. remove items from dock
  15. remove items from google search
  16. remove items from google drive
  17. remove items from photos
  18. remove items from pictures
  19. remove items from buy again amazon
  20. remove items from quick access

I am using Jquery to alter the source of an image as the page scrolls. However, currently they are loading as they are displayed and i would like to have them preload several images prior to being ...

I am using Jquery to alter the source of an image as the page scrolls. However, currently they are loading as they are displayed and i would like to have them preload several images prior to being ...