You must Sign In to post a response.
  • Category: JQuery

    How to make jquery validation for dynamically created controls

    hi all
    below is my code and my div that jquery calls and that will add row with one dropdown and textbox so i f i not selected dropdown and click on submit button then it should show please select any value


    var rc = 1;


    var Lid = 1;
    function addNextRow(currentEle) {

    if(rc>9)
    {
    alert("ly 10 app alwd");
    }
    else{
    rc++;
    Lid++;
    $("#maintable tr:last").after("<tr id=lineRow_"+Lid+" class='data-contact-person'><td colspan=5></td><td>"+$("#hdnSupplier").html()+"</td><td id=ftd><input name='Approver1Name' class='form-control Approver1Name' style='width: 100%; float: left;' data-val='true' id='Approver1Name"+Lid+"' readonly='true' required='required' value=''></td><td> <a href='#'><img src='@Url.Content("~/images/plus.jpg")' id='AddAttr' width=16px height=16px title='Add Approver' onclick='addNextRow(this);return false;'></a><a href='#'><img src='@Url.Content("~/images/cross.jpg")' width=16px height=16px title='Remove Approver' onclick='removerow(this);return false;'></a></td></tr>");
    }

    }
    """"""""""""this is div""""""""""""""""""
    <div id="hdnSupplier" style="display:none;">
    @Html.DropDownListFor(model => model.Approver1, (IEnumerable<SelectListItem>)ViewBag.Approver, "Select", new { Name = "Approver1", onchange = "getApprover1(this.options[this.selectedIndex].value,this);", @class = "form-control Login01 required", required = "required", style = "width:100%" })
    </div>
  • #769115
    We cannot apply Jquery validation to dynamic controls added on the unique name/id . Here is an example of code to guide how to make jquery validation for dynamically created controls
     constraint.$(.required).each(function(){ $(this).rules('add', {required:true, messages:{required: 'Required Field'}});});


Sign In to post your comments