0

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.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Maulik Shah
  • 1,040
  • 1
  • 7
  • 17
  • You can NOT validate multiple items with same `name`. It's a limitation of the plugin and discussed many times. You must index them as `name=name[1]`, `name=name[2]`, `name=name[3]`, etc. – Sparky Apr 22 '19 at 17:48
  • @Sparky I have checked solution that you mention as to mark my question as duplicate. None of that solution work in my case so i have asked question again. If you have solution let me know and help me as those other solution wont work for me. [https://stackoverflow.com/questions/35811125/how-to-add-validation-rule-dynamically-to-checkbox-array] This also not worked. – Maulik Shah Apr 30 '19 at 05:52

0 Answers0