Jquery function to select tab with required fields not filled

Asked

Viewed 409 times

0

I use the tab of bootstrap and the required of dataannotations, but the following happens, not to know the required fields, when it is not in the tab of him, for example, I’m in tab 1, and I conclude, but in the 2 fields with required are not filled, can not know, I would like to go to tab where the fields with required were not completed.

I tried this code both on submit and with the click of button, but it didn’t work:

$('#btnConcluir').click(function () {
        $('input:invalid').each(function () {
            // Find the tab-pane that this element is inside, and get the id
            var $closest = $(this).closest('.tab-pane');
            var id = $closest.attr('id');

            // Find the link that corresponds to the pane and have it show
            $('.nav a[href="#' + id + '"]').tab('show');

            // Only want to do it once
            return false;
        });
    });

I tried this one too:

 $('#btnConcluir').click(function () {
        console.log('entrou');
        $(':required:invalid', '#FornecedorNovo').each(function () {
            var id = $('.tab-pane').find(':required:invalid').closest('.tab-pane').attr('id');

            $('.nav a[href="#' + id + '"]').tab('show');
        });
    });
  • Mariana, the :invalid works with fields where you have the attribute required. I have no knowledge about this resource you are using, the dataannotations. If only for the required HTML5 that your code should work perfectly, and detail: that loop each not even necessary, just see if $('input:invalid', '#FornecedorNovo').length is true or false that it already finds the field empty.

  • @Sam I’m going to do a test this way, have some idea of some way that I can verify?

  • @sam I did the test and it didn’t work, the console it only validates the fields of the selected tab, and not of all.

2 answers

1

Follows a solution with Jquery Validation

$('#validate').validate({
  ignore: [],
  errorPlacement: function() {},
  submitHandler: function() {
    alert('Cadastro salvo com sucesso');
  },
  invalidHandler: function() {
    setTimeout(function() {
      $('.nav-tabs a small.required').remove();
      var validatePane = $('.tab-content.tab-validate .tab-pane:has(input.error)').each(function() {
        var id = $(this).attr('id');
        $('.nav-tabs').find('a[href^="#' + id + '"]').append(' <small class="required">*</small>');
        $('.nav-tabs').find('a[href^="#' + id + '"]').tab('show');
      });
    });
  },
  rules: {
    name: 'required',
    email: {
      required: true,
      email: true
    },
    zipcode: 'required',
    address: 'required',
    city: 'required'
  }
});
input.error {
  border-color: #f00 !important;
}

small.required {
  color: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container" style="margin-top: 20px;">

  <div class="panel panel-primary">
    <div class="panel-heading">
      Formulário de cadastro
    </div>
    <div class="panel-body">
      <form action="" class="form-horizontal" id="validate">
        <ul class="nav nav-tabs nav-justified nav-inline">
          <li class="active"><a href="#primary" data-toggle="tab">Informações do contato</a></li>
          <li><a href="#secondary" data-toggle="tab">Informações de endereço</a></li>
        </ul>


        <div class="tab-content tab-validate" style="margin-top:20px;">
          <div class="tab-pane active" id="primary">
            <div class="form-group">
              <label for="name" class="control-label col-md-2">Nome</label>
              <div class="col-md-10">
                <input type="text" name="name" class="form-control" />
              </div>
            </div>
            <div class="form-group">
              <label for="email" class="control-label col-md-2">E-mail</label>
              <div class="col-md-10">
                <input type="email" name="email" class="form-control" />
              </div>
            </div>
          </div>
          <div class="tab-pane" id="secondary">
            <div class="form-group">
              <label for="zipcode" class="control-label col-md-2">CEP</label>
              <div class="col-md-10">
                <input type="text" name="zipcode" class="form-control" />
              </div>
            </div>
            <div class="form-group">
              <label for="address" class="control-label col-md-2">Endereço</label>
              <div class="col-md-10">
                <input type="text" name="address" class="form-control" />
              </div>
            </div>
            <div class="form-group">
              <label for="city" class="control-label col-md-2">Cidade</label>
              <div class="col-md-10">
                <input type="text" name="city" class="form-control" />
              </div>
            </div>
          </div>
        </div>
        <div class="form-group col-md-2 pull-right">
          <button type="submit" class="btn btn-success btn-block">Salvar</button>
        </div>
      </form>
    </div>
  </div>

</div>

  • It does not select the tab that is missing to be filled, it has no way to do this ?

  • @marianac_costa. It does. I changed the answer and see if it meets you. Att,

  • Will I be able to adapt to check the required dataannotations ?

  • @marianac_costa you want to say required ASP NET MVC dataannotations? Or required HTML attribute?

  • From ASP.NET MVC.

0

Does not resolve by removing attributes from the element:

            $('#Salvar').removeAttr('type', 'submit');
            $('#Salvar').removeClass('data-toggle', 'title');
            $('#Salvar').addClass('disabled');
            $('#Salvar').removeAttr('type', 'submit');

And then just assign if all the required fields are filled in, another thing I did here at the company was create a function for the user to go to tab2 only if you have filled in all the fields in tab1, but in tab2 I did so and if it comes another more elegant way I will change to this solution.

  • I did not understand very well what Victor wanted to do Adriano, I need you to give Focus and select the tab where you have unfilled fields that have the required. In this example it just removes type Submit.

  • Then, you will have to create a function to know which tab index to check if the fields are filled from that tab.

  • I did the tests here Adriano, in the function I put in the question it only enters the tab selected, and I would like it to validate of all fields, so it is not working, it is recognizing only the fields of the tab selected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.