簡體   English   中英

將JSON用於jQuery驗證程序規則和消息

[英]Using JSON for jQuery validator rules and messages

我在http://jqueryvalidation.org使用jQuery Validate插件。

典型的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”:“ required”,“ email”:{“ required”:true,“ email”:true}但是除此之外,它應該可以工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM