2

I have basic javascript code to generate input text areas as below

$("#btnAdd").click(function (e) {
        var itemIndex = $("#container input.iHidden").length;
        e.preventDefault();
        var newItem = $("<span class='badge badge-success'>" + itemIndex + "</span> <input id='Interests_" + itemIndex + "__Id' type='hidden' value='' class='iHidden'  name='Interests[" + itemIndex + "].Id' /><input type='text' id='InvoiceNumber_" + itemIndex + "__InvoiceNumber' placeholder='Fatura Numarası' name='[" + itemIndex + "].InvoiceNumber'/>    <input type='text' id='Interests_" + itemIndex + "__InterestText' placeholder='Fatura Tutarı(TL)' name='[" + itemIndex + "].Amount'/> <br /><br />");
        $("#container").append(newItem);
    });

And i have a form for this dynamic fields. I am using jquery validator for this form for other elements. Now i also want to validate this dynamicly created fields.

For static fields here my working validation scripts.

  $('#frm_register').validate({

        focusInvalid: false,
        ignore: "",
        rules: {
            FirstName: {
                required: true
            } ....

And here sample of my dynamic fields.

<input type="text" id="InvoiceNumber_0__InvoiceNumber" placeholder="Fatura Numarası" name="[0].InvoiceNumber">
<input type="text" id="Interests_0__InterestText" placeholder="Fatura Tutarı(TL)" name="[0].Amount"> 
<input type="text" id="InvoiceNumber_1__InvoiceNumber" placeholder="Fatura Numarası" name="[1].InvoiceNumber">
<input type="text" id="Interests_1__InterestText" placeholder="Fatura Tutarı(TL)" name="[1].Amount"> 

1 Answer 1

3

You could use the .rules('add') method immediately after the new input element is created...

$("#btnAdd").click(function (e) {
    var itemIndex = $("#container input.iHidden").length;
    e.preventDefault();
    var newItem = $("<span class='badge badge-success'>" + itemIndex + "</span> <input id='Interests_" + itemIndex + "__Id' type='hidden' value='' class='iHidden'  name='Interests[" + itemIndex + "].Id' /><input type='text' id='InvoiceNumber_" + itemIndex + "__InvoiceNumber' placeholder='Fatura Numarası' name='[" + itemIndex + "].InvoiceNumber'/>    <input type='text' id='Interests_" + itemIndex + "__InterestText' placeholder='Fatura Tutarı(TL)' name='[" + itemIndex + "].Amount'/> <br /><br />");
    $("#container").append(newItem);

    // add the rules to your new item
    $('Interests_' + itemIndex + '__Id').rules('add', {
        // declare your rules here
        required: true
    });
});

Alternatively, for a simple rule like required, you could just add the required="required" attribute to the new element when you create it...

$("#btnAdd").click(function (e) {
    var itemIndex = $("#container input.iHidden").length;
    e.preventDefault();
    var newItem = $("<span class='badge badge-success'>" + itemIndex + "</span> <input id='Interests_" + itemIndex + "__Id' type='hidden' value='' class='iHidden'  name='Interests[" + itemIndex + "].Id' /><input type='text' id='InvoiceNumber_" + itemIndex + "__InvoiceNumber' placeholder='Fatura Numarası' name='[" + itemIndex + "].InvoiceNumber'/>    <input type='text' id='Interests_" + itemIndex + "__InterestText' placeholder='Fatura Tutarı(TL)' name='[" + itemIndex + "].Amount' required='required' /> <br /><br />");
    $("#container").append(newItem);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. How can i add rules for : if (InvoiceNumber_" + itemIndex + "__InvoiceNumber) is not null, [" + itemIndex + "].Amount must be required ?
@umki, not sure what you're asking. Just call .rules('add') to dynamically add rule(s) at any time.
I am trying to ask : There are two coloums in every row, and i want to perform conditional validation. If the field1 is not null, field2 is required. But if field is null, field2 is not required.
@umki, you would apply a conditional rule using the depends property. Search this page for "depends": jqueryvalidation.org/validate

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.