I am using jquery validation plugin. I have an 'add file field' button which will add new file fields dynamically on click. I want to validate these fields on submit. How can I do this ? Please help..
A:
You have to use jQuery's live or delegate methods http://api.jquery.com/live/ to bind the inputs created with a new validation rule.
E.g. (untested)
$('#addFieldButton').live('click', function() {
$('#myFormId').append('<input type="text" id="newfield" name="newfield" />');
$('#newfield').rules('add', {
minlength: 2
});
}
davidosomething
2010-04-28 15:31:58
Thanks David...now it works...:)
Aneesh
2010-04-30 03:35:20
A:
Never used the plugin, but I believe you need to apply the .validate();
method to any elements created after document/DOM load.
$newFileInput=$('<input type="file" name="files[]" />');
$newFileInput.appendTo($('form[name=newFiles]')).validate();
Name tags used are exemplary.
Zlatev
2010-04-28 15:32:32
A:
When you create the new element, just after your insert it, you just add the rules then, using .rules('add',rules)
, like this:
newField.YouJustCreatedrules("add", { required: true });
Nick Craver
2010-04-28 23:18:10