0

Problem

There is an array of input text fields. JQuery validate only first textbox. Below is my code. Am I missing anything?

Html is below

<form class="panel-body" id="UpdateProfile">
    <div class="row">
        <div class="col-md-10">
            <div class="form-group">
                <input type="text" value="" name="VideoUrl[]">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            <div class="form-group">
                <input type="text" value="" name="VideoUrl[]">
            </div>
        </div>
    </div>
    <input type="submit">
</form>

JQuery

<script>
    $(document).ready(function() {
        $("form#UpdateProfile").validate({
            ignore: [],
            rules: {
                'VideoUrl[]': "required"
            },
            messages: {
                'VideoUrl[]': "Please enter Video Url"
            },
            submitHandler: function(form) {
                var VideoUrls = [];
                $.each($('input[name="VideoUrl[]"]'), function(index, row){
                    var data = {
                        "VideoUrl": row.value
                    };
                    VideoUrls.push(data);
                });                
                var data = {
                    "VideoUrls": VideoUrls
                };
                console.log(data);
            }
        });
    });
</script>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • That's just how it works. When you define a rule within the `rules` object, each field must be specified by its **unique** `name`. You cannot use any `name`, even an array name, if it appears on more than one field. Otherwise only the first occurrence is validated. The only exception is radio and checkbox groups. – Sparky Jul 06 '17 at 01:24
  • Is there any way to validate all input type text fields? – Pankaj Jul 06 '17 at 01:27
  • Yes, however, you still **must** have a unique `name` on each field. [It's a mandated requirement of this plugin](https://jqueryvalidation.org/reference/#link-markup-recommendations). – Sparky Jul 06 '17 at 01:28

0 Answers0