0

I want validate my table if empty. my table using datatables to add data.

<form method="post" id="form">
  <input type="text" id="txt" required>
  <table id="table">
    <thead>
      <tr>
        <th>No.</th>
        <th>Name</th>
        <th>Address</th>
      </tr>
    </thead>
  </table>
<button id="button" type="submit">Save</button>
</form>

my jquery:

$(document).ready(function () {
  var grid = $('#table').DataTable();

  grid.row.add([
    '1',
    'Jake',
    'England'
  ]).draw(false);

  $("#form").validate({
    submitHandler: function (form) {
      form.submit();
    }
  });
});

If input. I'm just add required attribute. but how to check datatables if the table empty. If I'm not add some item. It's will be return message error in above table or top table

NB: this how to check datatables empty table.data().any()

1 Answer 1

1

You can add a hidden dummy input element. For example:

<input type="hidden" name="table_required">

Then use custom function as one of your validation rules.

$("#form").validate({
   rules: {
      table_required: {
         required: function(){
            return $('#table').DataTable().data().any();
         }
      }
   },
   submitHandler: function (form) {
      form.submit();
   }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.