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>
3
  • 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. Commented Jul 6, 2017 at 1:24
  • Is there any way to validate all input type text fields? Commented Jul 6, 2017 at 1:27
  • Yes, however, you still must have a unique name on each field. It's a mandated requirement of this plugin. Commented Jul 6, 2017 at 1:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.