Adding jQuery validator rules to dynamically created elements in ASP

后端 未结 3 953
面向向阳花
面向向阳花 2020-12-02 13:15

I\'ve got some dynamically inserted form fields on a page in an MVC3 project. Normally we would add the jQuery validation server-side, but in this case we can\'t (multiple

3条回答
  •  孤街浪徒
    2020-12-02 13:54

    As it turns out, this can be done mostly in HTML by adding a few attributes to each form element:

    • A name attribute
    • data-val="true"
    • data-val-required="message"

    Like so:

    
    

    Then the form just needs to be re-parsed via JS:

    //Remove current form validation information
    $("form")
        .removeData("validator")
        .removeData("unobtrusiveValidation");
    
    //Parse the form again
    $.validator
        .unobtrusive
        .parse("form");
    

提交回复
热议问题