views:

34

answers:

3

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
Thanks David...now it works...:)
Aneesh
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
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
Thanks Nick...it is working now...thanks alot..
Aneesh