0

I have this script:

http://jsfiddle.net/5RrGa/1619/

    $(document).ready(function () {

            $('#prihlaska').validate({ // initialize the plugin
                rules: {
                    jmeno: {
                        required: true
                    },
                    prijmeni: {
                        required: true
                    },
                    email: {
                        required: true,
                        email: true
                    }
                },
                messages: {
                    jmeno: {
                        required: "Vyplňte vaše jméno."
                    },
                    prijmeni: {
                        required: "Vyplňte vaše příjmení."
                    },
                    email: {
                        required: "Vyplňte vaši emailovou adresu.",
                        email: "Zadejte platnou emailovou adresu."
                    },
                    jmeno_ucastnik: {
                        required: "Vyplňte jméno účastníka.",
                    }
                }

            });

        });

 $("select[name=listku]").change(function() {
    var pocetInputu = $("#jmena input").length;
    var pozadovanyPocet = $(this).val()-1;
    rozdil = pozadovanyPocet - pocetInputu;
    if(rozdil > 0)
    {
        for (var i = 0; i < rozdil; i++) {
        $("#jmena").append('<div class="form-group">' +
                            '<label for="jmeno" class="sr-only">Jméno účastníka</label>' +
                            '<input type="text" class="form-control"  name="jmeno_ucastnik[]" placeholder="Jméno účastníka">' +
                           '</div>');
        }
    }
    else
    {
        $("#jmena input").slice(pozadovanyPocet).remove();
    }
})

Where I can add new input with name="jmeno_ucastnika[]" by JQuery, but for some unknown reason the validation for this input is not working (for input with name jmeno, prijmeni and email working).

2 Answers 2

1

You are adding control dynamically. So it is better that make a validate function and after adding the new controls, bind the validate event again with new element or with all elements. It is upto you.

You can do something like this.

$(document).ready(function () {
doValidate();
        });


function doValidate(){
console.log("doValidation");
            $('#prihlaska').unbind("validate").validate({ // initialize the plugin
                rules: {
                    jmeno: {
                        required: true
                    },
                    prijmeni: {
                        required: true
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    jmeno_ucastnik: {
                        required: true
                    }
                },
                messages: {
                    jmeno: {
                        required: "Vyplňte vaše jméno."
                    },
                    prijmeni: {
                        required: "Vyplňte vaše příjmení."
                    },
                    email: {
                        required: "Vyplňte vaši emailovou adresu.",
                        email: "Zadejte platnou emailovou adresu."
                    },
                    jmeno_ucastnik: {
                        required: "Your Validation Text here.",
                    }
                }

            });

}

 $("select[name=listku]").change(function() {
    var pocetInputu = $("#jmena input").length;
    var pozadovanyPocet = $(this).val()-1;
    rozdil = pozadovanyPocet - pocetInputu;
    if(rozdil > 0)
    {
        for (var i = 0; i < rozdil; i++) {
        $("#jmena").append('<div class="form-group">' +
                            '<label for="jmeno" class="sr-only">Jméno účastníka</label>' +
                            '<input type="text" class="form-control"  name="jmeno_ucastnik" placeholder="Jméno účastníka">' +
                           '</div>');
        }

    }
    else
    {
        $("#jmena input").slice(pozadovanyPocet).remove();
    }
    doValidate();
});

The jsFiddle is updated. You can test it there too.

http://jsfiddle.net/5RrGa/1620/

Sign up to request clarification or add additional context in comments.

4 Comments

This validate just first input with name="jmeno_ucastnik"
I know. But I have given you hint that how can you perform this activity. If you want on all controls then you can customize your doValidate() function with some parameters.. Else you can extend the validate parameter object (by making it global) and then call again the doValidate() fucntion then it will work definitely.
Yeah, i know what you mean (I hope :) ) - there is my edit: jsfiddle.net/5RrGa/1621. But still I can validate input with name="jmeno_ucastnik_1" (2, 3, ...)
You have to properly add the attributes to your validate function object. check now and upvote for the answer :-) jsfiddle.net/5RrGa/1622
0

jquery.validate requires that every input have a unique name. So instead of having names like

jmeno_ucastnik[]

you need to put explicit counters into the brackets. The initial HTML should use name=jmeno_ucastnik[0], and the Javascript that adds rows can increment it.

var counter = 0;
$("select[name=listku]").change(function() {
var pocetInputu = $("#jmena input").length;
var pozadovanyPocet = $(this).val()-1;
rozdil = pozadovanyPocet - pocetInputu;
if(rozdil > 0)
{
 counter++;
    for (var i = 0; i < rozdil; i++) {
    $("#jmena").append('<div class="form-group">' +
                        '<label for="jmeno" class="sr-only">Jméno účastníka</label>' +
                        '<input type="text" class="form-control"  name="jmeno_ucastnik['+counter+']" placeholder="Jméno účastníka">' +
                       '</div>');
    }
}
else
{
    $("#jmena input").slice(pozadovanyPocet).remove();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.