Validate Dynamically Added Input fields

前端 未结 10 843
北恋
北恋 2020-11-29 01:39

I have used this jquery validation plugin for the following form.




        
相关标签:
10条回答
  • 2020-11-29 02:13

    Try using input arrays:

    <form action="try.php" method="post">
        <div id="events_wrapper">
            <div id="sub_events">
                <input type="text" name="firstname[]" />                                       
            </div>
        </div>
        <input type="button" id="add_another_event" name="add_another_event" value="Add Another" />
        <input type="submit" name="submit" value="submit" />
    </form>
    

    and add this script and jQuery, using foreach() to retrieve the data being $_POST'ed:

    <script>                                                                                    
        $(document).ready(function(){
            $("#add_another_event").click(function(){
            var $address = $('#sub_events');
            var num = $('.clonedAddress').length; // there are 5 children inside each address so the prevCloned address * 5 + original
            var newNum = num + 1;
            var newElem = $address.clone().attr('id', 'address' + newNum).addClass('clonedAddress');
    
            //set all div id's and the input id's
            newElem.children('div').each (function (i) {
                this.id = 'input' + (newNum*5 + i);
            });
    
            newElem.find('input').each (function () {
                this.id = this.id + newNum;
                this.name = this.name + newNum;
            });
    
            if (num > 0) {
                $('.clonedAddress:last').after(newElem);
            } else {
                $address.after(newElem);
            }
    
            $('#btnDel').removeAttr('disabled');
            });
    
            $("#remove").click(function(){
    
            });
    
        });
    </script>
    
    0 讨论(0)
  • In regards to @RitchieD response, here is a jQuery plugin version to make things easier if you are using jQuery.

    (function ($) {
    
        $.fn.initValidation = function () {
    
            $(this).removeData("validator");
            $(this).removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse(this);
    
            return this;
        };
    
    }(jQuery));
    

    This can be used like this:

    $("#SomeForm").initValidation();
    
    0 讨论(0)
  • 2020-11-29 02:17

    Reset form validation after adding new fields.

    function resetFormValidator(formId) {
        $(formId).removeData('validator');
        $(formId).removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse(formId);
    }
    
    0 讨论(0)
  • 2020-11-29 02:18

    You should have 'name' attribute for your inputs. You need to add the rules dynamically, one option is to add them when the form submits.

    And here is my solution that I've tested and it works:

    <script type="text/javascript">
       $(document).ready(function() {
            var numberIncr = 1; // used to increment the name for the inputs
    
            function addInput() {
                $('#inputs').append($('<input class="comment" name="name'+numberIncr+'" />'));
                numberIncr++;
            }
    
            $('form.commentForm').on('submit', function(event) {
    
                // adding rules for inputs with class 'comment'
                $('input.comment').each(function() {
                    $(this).rules("add", 
                        {
                            required: true
                        })
                });            
    
                // prevent default submit action         
                event.preventDefault();
    
                // test if form is valid 
                if($('form.commentForm').validate().form()) {
                    console.log("validates");
                } else {
                    console.log("does not validate");
                }
            })
    
            // set handler for addInput button click
            $("#addInput").on('click', addInput);
    
            // initialize the validator
            $('form.commentForm').validate();
    
       });
    
    
    </script>
    

    And the html form part:

    <form class="commentForm" method="get" action="">
        <div>
    
            <p id="inputs">    
                <input class="comment" name="name0" />
            </p>
    
        <input class="submit" type="submit" value="Submit" />
        <input type="button" value="add" id="addInput" />
    
        </div>
    </form>
    

    Good luck! Please approve answer if it suits you!

    0 讨论(0)
提交回复
热议问题