How to use `Jquery-form-validator` for validation of form with tabs?

230 views Asked by At

I have use Jquery Form Validator plugin for validation of my Form from demo link. Now I seprated form with diffrent tab. How can i achieve validation on certain tab while clicking on NEXT BUTTON of the certain tab? what customisation should i make to the plugin method?

Here is the snippet,

$("ul.kyc-tab-list li a").click(function() {
  $("ul.kyc-tab-list li a").removeClass("active");
  $(this).addClass("active");
  /* Do this instead of multiple listeners */
  $(".kyc-tab-content").hide();
  $($(this).attr('class').split(' ').find(function(e) {
    return e.startsWith('tab-btn');
  }).replace('tab-btn', '#tab')).show();
});

// active tab load initially
$(".tab.active").trigger('click');


$('#btnNext').click(function() {
  var currentTab = $(".tab.active");
  var newTab = currentTab.closest('li').next().find('a');
  if (newTab.length === 0) {
    newTab = $(".tab").first();
  }
  newTab.trigger('click');
});

$('#btnPrevious').click(function() {
  var currentTab = $(".tab.active");
  var newTab = currentTab.closest('li').prev().find('a');
  if (newTab.length === 0) {
    newTab = $(".tab").last();
  }
  newTab.trigger('click');
});

var isAllvalid = true;

  $.validate({

    form: '#registration',
    modules: 'date, security, file, logic',
    validateOnBlur: true,
    showHelpOnFocus: true,
    addSuggestions: true,
    onModulesLoaded: function () {
        console.log('All modules loaded!');
    },
    onSuccess: function ($form) {
        //form.submit();
        //event.preventDefault();

        if (isAllvalid == true) {

            $.ajax({
                url: '@Url.Action("GenerateReport", "StaticPage")',
                type: 'POST',
                data: $('#registration').serialize(),
                success: function (data) {

                    if (data.fileName != "") {
                        window.location = '/StaticPage/Download?file=' + data.fileName;
                    }
                }
            });

        }

        return false;
    },
    onError: function () {
        //alert("Error")
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/theme-default.min.css" />

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>

<div>
  <section>
    <div class="kyc-tab-wrapper">
      <ul class="kyc-tab-list">
        <li>
          <a href="javascript:void(0);" class="tab active tab-btn1"><span>01</span>Customer</a>
        </li>
        <li>
          <a href="javascript:void(0);" class="tab tab-btn2"><span>02</span>Contact / Promotors</a>
        </li>
        <li>
          <a href="javascript:void(0);" class="tab tab-btn3"><span>03</span>Bank / Business</a>
        </li>
        <li>
          <a href="javascript:void(0);" class="tab tab-btn4"><span>04</span>Other</a>
        </li>
        <li>
          <a href="javascript:void(0);" class="tab tab-btn5"><span>05</span>Finish</a>
        </li>
      </ul>
      <div class="kyc-tab-content" id="tab1">tab 1</div>
      <div class="kyc-tab-content" id="tab2">tab 2</div>
      <div class="kyc-tab-content" id="tab3">tab 3</div>
      <div class="kyc-tab-content" id="tab4">tab 4</div>
      <div class="kyc-tab-content" id="tab5">tab 5</div>
      <div class="kyc-tab-form-btn-wrap">
        <button id="btnPrevious">  Previous</button>
        <button id="btnNext">Next </button>
      </div>
    </div>
  </section>
</div>

What customize logic should i implement into $.validate({}) method?

0

There are 0 answers