jquery validate plugin combine field validation
On one form I have City, State, ZIP code on one line in the fo开发者_如何学运维rm (inside one block element).
My error div for the validate plugin slides nicely under the form div so that message is contextual.
Is there a way to combine a rule so that it's not 3 separate validation rules? In other words, if city is blank, or state is blank, or ZIP is blank, I want to trigger just one error message div "City, State and ZIP code are required". Likewise if 2 of 3 or 3 of 3 are blank I still want to have just one error div returned by validate.
You can make use of the groups
and errorPlacement
option to your needs.
For example (taken from http://docs.jquery.com/Plugins/Validation/validate):
$("#myform").validate({
groups: {
username: "fname lname"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "fname"
|| element.attr("name") == "lname" )
error.insertAfter("#lastname");
else
error.insertAfter(element);
},
debug:true
});
精彩评论