3

I have a form $('#myForm') that has two divs $('#myDiv1'), $('#myDiv2'), with two text fields in each. I would like a single errorContainer for each of the two divs.

If I call $('#myForm').validate({ ... }); I can only specify a single errorContainer, which isn't what I want.

If I call $('#myDiv1').validate({ ... }); I get an error from within jquery-validate: Uncaught TypeError: Cannot read property 'settings' of undefined. This leads me to presume that I cannot call validate on anything but a form.

Is there a way I can do what I want?

1
  • 1
    Just an FYI: $('form').validate() is called once on DOM ready to initialize the plugin on the form. It should not be called multiple times and, as you already discovered, you cannot call it on an element. Commented Feb 5, 2013 at 5:29

2 Answers 2

1

You can use the following:

$("#myform").validate({
   errorPlacement: function(error, element) {}
}); 

Where the 'element' is the jQuery object of the current form input. It's in http://docs.jquery.com/Plugins/Validation/validate#toptions

Sign up to request clarification or add additional context in comments.

Comments

0

The validate method must be called on a form element.

I haven't used this in practice, but the options docs seem to indicate you can pass multiple selectors for the errorContainer option.

$("#myform").validate({
   errorContainer: "#messageBox1, #messageBox2",
   errorLabelContainer: "#messageBox1 ul",
   wrapper: "li", debug:true,
   submitHandler: function() { alert("Submitted!") }
}); 

1 Comment

Additional markup is required for your code to work as intended in the doc's example. Regardless, still not quite what the OP is asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.