I am trying to validate input tags which i am adding dynamically using jquery append. It is only validate first input of name which has name=name[] and not validate remaining. I have tried with below code.
$("#inventory_form").validate({
rules: {
asset_type: {
required: true,
},
inventory_name: {
required: true,
}
});
$('#inventory_form').on('submit', function (event) {
$('#inventory_form .inputValue').each(function () {
$(this).rules("add",
{
required: true,
})
});
});
Here dynamicInput
is used to add dynamically input tags in form html.
function dynamicInput(selector) {
var html = '';
console.log(selector)
var assetData = '<?php echo $assetTypes ?>';
var relation = JSON.parse(assetData);
$.each(relation, function (key, value1) {
if (($('#asset_type').val()) == value1['id']) {
$.each(value1['asset_items'], function (key, value) {
html += '<div class="form-group">' +
'<label for="" class="text-capitalize">' + value['name'] + '</label>' +
'<input type="' + (value['type'] == 'date_range' ? 'text' : value['type']) + '" name="' + value['name'] + '" class="form-control inputValue" value="{{ old('value')}}"' + (value['type'] == 'date_range' ? 'id="customDate"' : "") + '>' +
'</div>'
$(this).rules('add', {
required: (value['type'] == 0 ? false : true),
});
});
$(selector).append(html).show()
// For dynamic type (Inventory)
$('#customDate').daterangepicker();
}
});
}
I want to validate all the dynamically added input box which has name=name[]. Please help me where i am doing wrong.