jQuery validate and groups - how to avoid errorPlacement usage?

时间:2021-06-08 23:56:21

本文参照:https://*.com/questions/14146893/using-groups-with-jquery-form-validation-how

指定错误提示的位置

<form id="myform">
First Name: <input type="text" name="fname" id="fname"/><br>
Last Name: <input type="text" name="lname" id="lname"/><br> <input type="submit"/>
</form>​

And this validate code:

$("#myform").validate({
rules: {
fname: { required: true },
lname: { required: true }
},
groups: {
username: "fname lname"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "fname"
|| element.attr("name") == "lname" )
error.insertAfter("#lname");
else
error.insertAfter(element);
}
})​