欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery validate and groups - how to avoid errorPlacement usage?

程序员文章站 2024-02-03 11:41:58
本文参照:https://*.com/questions/14146893/using-groups-with-jquery-form-validation-how 指定错误提示的位置 And this validate code: ......

本文参照:

 

指定错误提示的位置

<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);
   }
 })​