Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate on elements not yet created

I'm wondering if it's possible to have the jQuery Validator plugin validate elements that don't yet exist in the dom when the rules are initially setup.

Calling the .Rules("add", therules) method only attaches the rules to elements that currently exist in the dom. If I were to create some. The validation doesn't fire. Any thoughts?

Homepage of the validator I am using: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

New elements are being created via http://ejohn.org/blog/javascript-micro-templating/

Basically there's an ajax call and the server returns a bunch of json (ajaj?), this json is fed through with the resig's templating engine. An example template looks like so:

<script type="text/html" id="ProductsTemplateEdit">
    <td>
        <input type="hidden" value="<#= item.ID #>" id="Edit.ID" name="Edit.ID" />
        <input type="text" value="<#= item.Price#>" id="Edit.Price" name="Edit.Price" />
    </td>
</script>

The values are obviously filled in via the json that is passed in.

I should also mention that I am using the xval validation framework, which basically automatically generates the jquery validate rules for me: http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/

like image 841
ChadT Avatar asked Mar 23 '09 03:03

ChadT


1 Answers

Okay, try two. What is adding your elements dynamically? Can't you just place the .Rules("add", ) to the same code?

like image 187
Nicholas Head Avatar answered Oct 05 '22 09:10

Nicholas Head