将JSON用于jQuery验证器规则和消息

Using JSON for jQuery validator rules and messages

本文关键字:规则 消息 验证 JSON 用于 jQuery      更新时间:2023-09-26

我在http://jqueryvalidation.org.

典型的jconfiguration可能如下所示:

$(".selector").validate({
    rules: {
        name: "required",
        email: {
            required: true,
            email: true
        }
    },
    messages: {
        name: "Please specify your name",
        email: {
            required: "We need your email address to contact you",
            email: "Your email address must be in the format of name@domain.com"
        }
    }
});

我不希望将规则和消息包含在JavaScript文件中,而是希望将它们包含在一个单独的JSON文件中。请注意,此问题不适用于使用远程方法。

有没有具体的方法可以做到这一点,或者我必须做以下事情:

$.getJSON("getValidation.json", function(json) {
    $(".selector").validate(json);
});

我认识到,如果规则需要回调,这种方法将不起作用,因为它会破坏JSON。

您的JSON无效,JSON属性应使用引号"rules":{"name":"必需","电子邮件":{"必需":true,"电子邮件":true}但除此之外,它应该可以在

中工作