How to only change to step 2 if there are no validation errors in the step 1?

How to only change to step 2 if there are no validation errors in the step 1?

I'm trying to create a multi-step form using jQuery AJAX and I want to validate each step with an ajax post request. 

In the step 1 form, when the "go to step 2" button is clicked if there is a validation error in the console network tab it appears:


{success: false, errors: {buyer_name: ["The buyer name must be greater than 1 character."]}}

    1. errors:{buyer_name: ["The buyer name must be greater than 1 characters."]}
    2. success:false
This is ok. But even this error appears the user goes to the "step 2" tab. Then, in the next step 2 form if the user clicks in "go back to step 1" the validation errors appear in the "#step1Errors" div.

But do you know what is necessary to only change to the "step 2" tab, and only allow the user to change to the "step 2" tab if there are no validation errors in the ajax post request after click in "go to step 2" in the "step 1" tab?


I have the navigation between steps in the fiddle:  https://jsfiddle.net/3adebaL3/10/. Ajax:

  
  1. $('#goToStep2').on('click', function (event) {
  2. event.preventDefault();


  3. var custom_form = $("#" + page_form_id);

  4. $.ajax({
  5. method: "POST",
  6. url: '/product/storeUserInfo',
  7. data: custom_form.serialize(),
  8. datatype: 'json',

  9. success: function (data, textStatus, jqXHR) {
  10. setTimeout(function () {

  11. }, 3000);
  12. },
  13. error: function (data) {
  14. console.log(data);
  15. var errors = data.responseJSON;
  16. var errorsHtml = '';
  17. $.each(errors['errors'], function (index, value) {
  18. errorsHtml +=
  19. '<ul class="alert alert-danger mt-3">
  20. <li class=text-danger">' + value + '</li>
  21. </ul>';
  22. });

  23. $('#step1Errors').show().html(errorsHtml);
  24. }
  25. });

  26. });
  27. });