如何组合来自jquery验证的错误消息

时间:2014-08-19 04:14:21

标签: javascript jquery jquery-validate

如何组合来自jquery验证的错误消息。例如:

messages: {
    first_name: { required: "Please enter your first name." },
    email: {
         required: "Please enter a valid email address.",
         remote: "Email has been registered."
    },
    username: {
         required: "Please enter username.",
         remote: "Username has been registered."
    }
 },

所需的消息将是这样的:

  

请输入您的名字,有效的电子邮件地址和用户名

  

请输入您的名字和用户名,电子邮件已经注册   等

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式执行此操作

您必须使用errorPlacement,并且两者的消息应相同,例如:

messages: {
first_name: { required: "Please enter your first name , valid email address and enter username ." },
email: {
     required: "Please enter your first name , valid email address and enter username .",
     remote: "Email has been registered."
},
username: {
     required: "Please enter your first name , valid email address and enter username .",
     remote: "Username has been registered."
 }
}

然后,假设它们在此处具有相同的ID,则errorPlacement选项将如下所示:

errorPlacement: {
 var n = element.attr("name");
 if (n == "first_name" || n == "email" || n == "username")
error.insertAfter("#username");
else
  error.insertAfter(element);
}

希望这可以帮到你