jquery验证插件动态规则

时间:2011-03-26 06:28:12

标签: jquery jquery-validate

我正在使用jQuery Validation插件,我想动态指定表单规则:

<input type="text" name="MyText" id="MyText" />
var Rule="Mytext";
$('#someForm').validate({
    rules: {
        Rule: {
            required: true
        }
    },
    messages: {
        Rule: {
            required: 'Please fill the MyText field'
        }
    }
});

1 个答案:

答案 0 :(得分:3)

var Rule = "Mytext";

var validation = {rules: {}, messages: {}};

validation.rules[Rule] = {
    required: true
};

validation.messages[Rule] = {
    required: 'Please fill the MyText field'
};

$('form').validate(validation);

jsFiddle