Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74020

Dynamically added row validation not working - jquery?

$
0
0

I trying to validate dynamically added rows but some faults its not working.. i could'nt find my problem.. anyone tell me or correct me why its not working n all..

I need to validate dynamically add rows and columns.

here is my full code..

Full code Fiddle <3

Here is my sample code..

$(document).ready(function() {
  $("#ok_button").on("click", function() {

    $(".act").val($("#cash_text:first").val());
  });
  $("#add_Row").on("click", function() {

    var counter = 0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    // alert(idVal);
    if (idVal === undefined) {
      counter = 0;
    } else {
      var matches = idVal.match(/\d+/g);
      // alert(matches);
      if (matches != null) {
        counter = Number(matches) + counter + 1;
      }
    }
    var newRow = $("<tr>");
    var cols = "";
    if ($('.price').length > 0) {
      var first_ele = document.querySelector('.price').value;
    } else {
      var first_ele = '';
    }
    cols += '<td><select class="form-control sel_sel status required" id="accountName' + counter + '" name="accountName" for="accountName" required><option value="">Choose an account</option><option value="1">code 1</option></select></td>';
    cols += '<td><input value="' + first_ele + '" type="text" class="form-control required price narr" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" data-toggle="modal" data-target="#narratModal"/></td>';

    cols += '<td><input type="number" class="form-control sumTot deb allignAmt" id="cashdeb' + counter + '" min="0" data-action="sumDebit" name="debit" placeholder="Debit amount" onkeypress="restrictMinus(event);" required/></td>';

    cols += '<td style="width: 11%;"><button type="button" class="adRow ibtnDel" style="width:30%;position: relative;right: 25%;">x</button></a></td>'

    newRow.append(cols);


    var defVal = $("select[name=acctname]").find(":selected").val();
    if (defVal) {
      $("select[name=accountName]").find(`option[value=${defVal}]`).hide();
    }
    $("table.order-list").append(newRow);
    setValCashVal('accountName'.concat(counter));
    bindScript();
    counter++;

  });
  $("table.order-list").on("click", ".ibtnDel", function(_event) {
    $(this).closest("tr").remove();
    if ($("#tab_logic tbody tr").length == 1)
      $("#cash_text:first").val('');
    evaluateTotal();

  });
});


// dynamic row validations

$(document).ready(function() {

  $('#contactForm').on('submit', function(event) {
    console.log($('.narr'))
    $('.status').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    $('.narr').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    $('.deb').each(function() {
      $(this).rules("add", {
        required: true
      })
    });
    event.preventDefault();
    if ($('#contactForm').validate().form()) {
      alert("validates");
    } else {
      alert("does not validate");
    }
  });
  $('#contactForm').validate();
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script><!-- validation cdn --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/css/bootstrapValidator.min.css"><script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.19.0/jquery.validate.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script><form id="contactForm"><input type="button" class="add_Row adRow" id="add_Row" value="Add Row"><table class="table table-bordered table-hover order-list" id="tab_logic" style="width:100% !important"><thead><tr style="background-color: #680779; color: #fff;"><th class="text-center">
          select code*</th><th class="text-center">
          description*</th><th class="text-center">
          amount*</th><th class="text-center">
          action*</th></tr></thead></table><!-- submit button --><div class="form-group col-4 vocSub" style="margin-bottom: 0px !important;"><div class="col-md-12 cashform_submit" id=""><input type="submit" class="btn add-btn submit-btn load cashmainBtn" id="cashSub" value="Submit" /></div></div></form>

I need to validate dynamically add rows and columns.

Thank you


Viewing all articles
Browse latest Browse all 74020

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>