Parsley: How to prevent validation when pressing buttons other than submit?

1.7k Views Asked by At

Thank you for any help.

I am trying to use Parsley for Form Validation. My form has one submit button and some other buttons to dynamically add inputs to the form. But when I press these other buttons, form validation is carried out. But I am not submitting any form.

How to prevent form validation from happening when I press other buttons than submit button?

Sorry, I dont know how to JS Fiddle. My code is like the following:

<form method="post" action="confirm" data-parsley-validate>
    <input id="brand" data-parsley-trigger="submit" required />
    <button id="addQuantity">Add</button>
    <input type="number" required data-parsley-trigger="submit" />
    <input type="submit" value="Submit">
</form>

When I press Add, the form is validated. How should I prevent this? Thank you very much.

2

There are 2 best solutions below

1
On BEST ANSWER

The tag button which was introduced in HTML5 is equivalent to input type="submit" hence when you press add it will automatically fire submit action. What you can do is replace the tag to input type="button" or you can prevent the default action in jquery like this

 <script> 
   $('#addQuantity').click(function(event)
    {
       event.preventDefault();
       //do your action goes below
    });
 </script>
0
On

I found adding formnovalidate to the button skipped form validation for the form only when clicking that button.