diff --git a/jquery.validate.js b/jquery.validate.js index c86bf6abe..d94cd25ff 100644 --- a/jquery.validate.js +++ b/jquery.validate.js @@ -480,6 +480,11 @@ $.extend($.validator, { console.error( "%o has no name assigned", this); } + // ignore elements of nested forms + if ( this.form !== validator.currentForm ) { + return false; + } + // select only the first element for each name, and only those with rules specified if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) { return false; diff --git a/test/index.html b/test/index.html index bb9fc4ad8..52ceecf6e 100644 --- a/test/index.html +++ b/test/index.html @@ -318,6 +318,10 @@

+ +
+ +
diff --git a/test/test.js b/test/test.js index cdfa3edb2..5571aa0a6 100644 --- a/test/test.js +++ b/test/test.js @@ -841,6 +841,18 @@ test('bypassing validation on form submission',function () { equal($v.numberOfInvalids(), 1, "Validation failed correctly"); }); +test("elements() should ignore elements of nested forms", function() { + var validator, + $form = $( "#enclosingForm" ); + + $( "
" ).appendTo( $form ); + $( "
" ).appendTo( $form ); + $form[ 0 ].uname.value = "Test"; + + validator = $form.validate(); + + equal( validator.elements().length, 1, "Elements of nested forms are ignored" ); +}); module("misc");