2

我正在尝试在表单上动态指定一些规则。在这个表单上,我有三个 TextBox 输入 ( Text1, Text2, Text3) 和一个复选框 ( Check1)

我将配置以下规则:

  1. 规则Text2:该字段应为2位小数的正数,其值应等于或大于Text1

  2. 规则Text3:该字段应为带有 2 位小数的数字。而且:

    a)如果选择,则 的值Text3必须等于 0Check1

    b) 的值Text3必须小于或等于Text1且小于或等于Text2如果Check1未选择。

如何在jQuery Validation插件中表达这些规则?有什么建议吗?

更新:这是 (1) 的基本规则。它错过了相等或更大的部分:

$('#Text2').rules('add', {
    required: true,
    min: 0.0,
    number: true,
    messages: {
        required: 'Message 1',
        min: 'Message 2',
        number: 'Message 3'
    }
});
4

1 回答 1

3

You can make use of $.validator.addMethod..

for example,

$.validator.addMethod("decimal", function(val, element){return /\d*(\.\d+)?/.test(val)}, "Should enter only numericals.");

the above method just check for decimal.

We can use this by adding the first parameter name in the class name such as

<input type="text" class="required decimal" />

In your case, you need to check for whether the value is greater than text1, so we can do it as

$.validator.addMethod("text2validator", function(val, element){if(val > $("#text1").val())return true;}, "Value should be greater than text1")

<input type="text" id="text1" value="10" />
<input type="text" id="text2" class="required decimal text2validator" />
于 2012-07-24T02:41:33.103 回答