0

Before reading this question, I want to tell that @Sparky suggested I check this link jQuery multiple input name[] validation but I can't declare the 18 fields manually in the script.

I have min 18 fields on my popup. I shared the example with 3 fields only. I am using jQuery validation and I am displaying fields dynamically. I have to set the validation of the dynamic field but validation is working only for the first fields, Not working with the dynamic field.

What I am doing, I have a list of records and when admin clicks on the Add button the model will open and then admin can fill the details. My validation is working only for the first field.

Please check my script I think there is some issue with my script It's not calling the script the second time because first, I am using

$(document).on('click', '.addexternal', function() { 

then calling the validation

$("#register").validate({

can anyone help me out with this issue?

I am using the below code.

var isReqInprogress = false;
$(document).on('click', '.addexternal', function() {
  var user_id = $(this).attr("data-id");
  $('#myModal').modal('show');

  $("#register").validate({
    ignore: [],
    rules: {
      "fullname[]": {
        required: true
      },
      "dev[]": {
        required: true
      },
      "details[]": {
        required: {
          depends: function() {
            if (($('.dev').val() == "3")) {
              return true;
            } else {
              return false;
            }

          }
        }
      }

    },
    errorPlacement: function(error, element) {
      if (element.is("select")) {
        error.insertAfter(element.parent());
      } else {
        error.insertAfter(element);
      }

    },
    submitHandler: function(form) {
      //form.submit();
      if (isReqInprogress) {
        return;
      }
      isReqInprogress = true;

      $.ajax({
        url: "process.php",
        type: "POST",
        dataType: "json",
        data: $('#register').serialize(), //i'll add user_id after some time

        success: function(data) {
          if (data.error == "true") {
            alert("added");
          } else {
            alert("error");
          }

          isReqInprogress = false;
        },
      }); // AJAX Get Jquery statment
    }

  });
});


/*duplicate form fields*/
$(document).ready(function() {
  var maxField = 10; //Input fields increment limitation
  var x = 1; //Initial field counter is 1
  var count = 2;
  //Once add button is clicked
  $(document).on('click', '#clicktoadd', function() {
    //alert("hello");        
    //Check maximum number of input fields
    if (x < maxField) {
      x++; //Increment field counter
      $(".modal-body").append('<div class="row"> <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12"> <div class="form-group"> <input type="text" name="fullname[]" class="form-control" placeholder="fullname"> </div></div><div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12"> <div class="form-group"> <select name="dev[]" class="form-control dev"> <option disabled="" selected="">Choose</option> <option value="1">Demo1</option> <option value="2">Demo2</option> <option value="3">Demo3</option> </select> </div></div><div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12"> <div class="form-group"> <input type="text" name="details[]" class="form-control"> </div></div></div><a href="javascript:void(0)" class="remove_button">Remove</a></div>');
    }
    count++;
  });
  //Once remove button is clicked
  $('.modal-body').on('click', '.remove_button', function(e) {
    e.preventDefault();
    $(this).parent('div').remove(); //Remove field html
    x--; //Decrement field counter
  });

});
.modal-dialog {
  max-width: 90% !important;
}

.new_add_bank {
  position: absolute;
  top: -50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<table class="table table-striped table-bordered" style="width:100%">
  <thead>
    <tr>
      <th>id</th>
      <th>fullname</th>
      <th>Dev</th>
      <th>details</th>
      <th>action</th>

    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Demo</td>
      <td>Demo1</td>
      <td>dgdfg</td>
      <td><a href="javascript:void(0)" class="addexternal btn btn-primary" data-id="101">Add</a></td>
    </tr>
    <tr>
      <td>2</td>
      <td>xzxc</td>
      <td>Demo2</td>
      <td>mnbvg</td>
      <td><a href="javascript:void(0)" class="addexternal btn btn-primary" data-id="102">Add</a></td>
    </tr>
  </tbody>
</table>

<!-- The Modal -->
<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Popup hading</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div id="clicktoadd"><a href="javascript:void(0);" class="btn btn-bg">Add More</a></div>
      <form method="post" action="#" name="register" id="register">
        <!-- Modal body -->
        <div class="modal-body">
          <div class="row">
            <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
              <div class="form-group">
                <input type="text" name="fullname[]" class="form-control" placeholder="fullname">
              </div>
            </div>

            <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
              <div class="form-group">
                <select name="dev[]" class="form-control dev">
                  <option disabled="" selected="">Choose</option>
                  <option value="1">Demo1</option>
                  <option value="2">Demo2</option>
                  <option value="3">Demo3</option>
                </select>
              </div>
            </div>

            <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
              <div class="form-group">
                <input type="text" name="details[]" class="form-control">
              </div>
            </div>
          </div>
        </div>
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" class="btn btn-danger">Submit</button>
        </div>
      </form>

    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
user9437856
  • 2,360
  • 2
  • 33
  • 92
  • @mplungjan, Where i have to use that code? – user9437856 Mar 26 '20 at 09:19
  • @mplungjan .You mean I have to use this code -> $("#formId").validator('update'); – user9437856 Mar 26 '20 at 09:24
  • Again, I cannot stress this enough. Yours is not a unique situation. Its been asked and answered many times before. There are about 6600 questions tagged with jQuery Validate and dozens, if not hundreds, that have managed to properly use dynamic naming arrays. Please carefully review the duplicates listed above your question. You can dynamically ADD the new field with a unique index number in the new `name[i]`, and then you would dynamically add the rules to this new field using the `.rules()` method. – Sparky Mar 26 '20 at 17:04
  • @Sparky, I already check all the links before uploading the question. That's the reason I am uplaing the same question again and again. I also mention your name in the question. – user9437856 Mar 27 '20 at 10:41
  • @Sparky, The solution which is in the duplicate answer that is totally different. Some of the answerss is a too old. – user9437856 Mar 27 '20 at 10:42
  • @Sparky, I am waiting for your reply. – user9437856 Mar 28 '20 at 05:12
  • My question is, I am using jquery validation inside $(document).on('click', '.addexternal', function() { and validation not working on dynamic field when submit. It's working only with first field. – user9437856 Mar 28 '20 at 18:52
  • Why my comment deleted? Is it wrong? – user9437856 Mar 29 '20 at 05:14

0 Answers0