Reputation: 158
Using jquery validation, is there a way to have each error message displayed in a different location.
I want to display the "required" message next to the email field, but the "invalid email address" in a validation summary div at the bottom of the form.
I know i can use errorPlacement but I think this is for all messages.
Upvotes: 1
Views: 769
Reputation: 702
Use the errorPlacement
method in .validate()
to contain the error placement logic. 2 arguements are passed to errorPlacement
:
You can use the error message returned by the validator to decide where it should be positioned. EG:
$("form").validate({
ignore: '',
errorPlacement: function(error, element){
if(error.text() == "This field is required"){
$('.requiredContainer').text(error.text());
}
else
{
$('.genericContainer').text(error.text());
}
});
});
If you're using this with multiple forms, I would suggest putting all of your validator settings in setDefaults()
and just using validate()
to handle rules/messages etc
Upvotes: 1