Solution 1 :

You need call .each() on the dynamically added elements as well.

Dynamically added elements are not in DOM onload so ideally you need to do wrap .each() in a function when you add more fields to your modal just call that function again to check for empty inputs

To handle submit and store data we can .submit on your modal form. Get all the data via .serialize method and send all your form data to the backend file via ajax request.

Run Snippet below to see it working.

$(function() {

  //Validate Data
  var validate = $("#newModalForm").validate()
  checkInput()

  //Add dynamic inputs
  $(document).on('click', '#add_field', function(e) {
    $('#dynamic_div').html("<div class=form-group><label class=control-label col-md-3 for=email>Dynamic field 1:</label> <input name=sfs type=text class=form-control></div> <div class=form-group><label class=control-label col-md-3 for=email>Dynamic field 2:</label><input name=ssf type=text class=form-control></div>");

    //Required  Dynamic Input
    checkInput()
  });

  //Validate all inputs
  function checkInput() {
    $('.form-control').each(function() {
      required($(this))
    });
  }

  //Required field message
  function required(el) {
    el.rules("add", {
      required: true,
      messages: {
        required: "This field is required",
      }
    });
  }

  //Submit form modal
  $('#newModalForm').on('submit', function(e) {

    //Prevent default submit behaviour
    e.preventDefault()

    //Store all the form modal form data 
    var data = $(this).serialize()

    //Check all fieild have data
    if (validate.errorList.length == 0) {
      alert('All fields have value - Form will submit now')
      //Request to backend
      $.ajax({
        url: 'your_url',
        type: 'POST',
        data: data,
        success: function(response) {
          //do something on success
        },
        error: function(xhr) {
          //Handle errors
        }
      });
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMyModal">Open Modal</button>
<div class="modal fade" id="addMyModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Stuff</h4>
      </div>
      <div class="modal-body">
        <form role="form" method="post" id="newModalForm">
          <div class="form-group">
            <label class="control-label col-md-3" for="email">A p Name:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="pName" name="pName" placeholder="Enter a p name" />
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-md-3" for="email">Action:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="action" name="action" placeholder="Enter and action">
            </div>
          </div>


          <div class="col-md-9" id="dynamic_div">

          </div>


          <div class="modal-footer">
            <button type="submit" class="btn btn-success" id="btnSaveIt">Save</button>
            <button type="button" id="add_field" class="btn btn-default">Add Field</button>
            <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

Problem :

I’m validating fields inside modal popup,for single field it is working but if more than one field are appended at a time then validation does not takes place on those fields. Here we can see we add new field by adding add field button but those newly field did’nt get validated.

$(function() {
    $("#newModalForm").validate();
    $('.form-control').each(function() {
        required($(this))
    });


    $(document).on('click', '#add_field', function(e) {
 $('#dynamic_div').html("<div class=form-group><label class=control-label col-md-3 for=email>Dynamic field 1:</label> <inputname=sfs type=text class=form-control></div> <div class=form-group><label class=control-label col-md-3 for=email>Dynamic field 2:</label><input name=ssf type=text class=form-control></div>");
        required($(this))
    });

    function required(el) {
        el.rules("add", {
            required: true,
            messages: {
                required: "This field is required",
            }
        });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMyModal">Open Modal</button>
<div class="modal fade" id="addMyModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Stuff</h4>
      </div>
      <div class="modal-body">
        <form role="form" id="newModalForm">
          <div class="form-group">
            <label class="control-label col-md-3" for="email">A p Name:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="pName" name="pName" placeholder="Enter a p name"/>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-md-3" for="email">Action:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="action" name="action" placeholder="Enter and action">
            </div>
          </div>
          
           
            <div class="col-md-9" id="dynamic_div">
              
            </div>
          
          
          <div class="modal-footer">
            <button type="submit" class="btn btn-success" id="btnSaveIt">Save</button>
            <button type="button" id="add_field" class="btn btn-default">Add Field</button>
            <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

Comments

Comment posted by user3653474

:Thanks can you please tell where i can write submit handler for form

Comment posted by Always Helping

@user3653474 let me update my answer simplify the answer more with submit function as well. Thanks

Comment posted by user3653474

Ok, Please add submit handler also

Comment posted by Always Helping

@user3653474 I have updated my answer to handle submit and store all data as and send to the backend via ajax.

By