2009-04-29 36 views
1

使用jQuery Validation plugin,以确保在第二个文本字段什么用户输入的积极cost_of_car在第一个文本字段,并积极支付金额禁用自定义的验证方法,例如什么* cost_of_car * 0.4 < =支付金额< = cost_of_car * :如何在条件

$.validator.addMethod("testMaxAmount", function() 
{ 
    return $("#cost_of_car").val() - 0 >= $("#payment_amount").val() - 0; 
}, "Can't be more than cost_of_car"); 

$.validator.addMethod("testMinAmount", function() 
{ 
    return $("#cost_of_car").val() * 0.4 <= $("#payment_amount").val() - 0; 
}, "Can't be less than cost_of_car * 0.4"); 

$("#calc_form").validate(
{ 
    rules: 
    { 
     cost_of_car: 
     { 
      required: true, 
      number: true, 
      min: 1, 
      max: 10000000 
     }, 
     payment_amount: 
     { 
      required: true, 
      number: true, 
      testMaxAmount: true, 
      testMinAmount: true 
     } 
    } 
}); 

现在我想跳过testMaxAmount和testMinAmount检查,直到cost_of_car是有效的。测试

$("#calc_form").validate().element("#cost_of_car") 

甚至

$("#calc_form").validate({ignore: "#payment_amount"}).element("#cost_of_car") 

这些方法里面导致递归和挂起的浏览器。

你提出一些其他方法来禁用支付金额的验证,直到cost_of_car是有效的吗?

回答

2

UPDATE:的改变必须是在validator.addMethod()电话:

$.validator.addMethod("testMaxAmount", function() 
{ 
    if($("#cost_of_car").valid()) 
     return $("#cost_of_car").val() - 0 >= $("#payment_amount").val() - 0; 
    else 
     return true; 
}, "Can't be more than cost_of_car"); 

$.validator.addMethod("testMinAmount", function() 
{ 
    if($("#cost_of_car").valid()) 
     return $("#cost_of_car").val() * 0.4 <= $("#payment_amount").val() - 0; 
    else 
     return true; 
}, "Can't be less than cost_of_car * 0.4"); 
+0

还不行。输入1000作为cost_of_car,输入500作为payment_amount - 没有预期的错误消息。输入300而不是500 - 出现“不能少于cost_of_car * 0.4”的信息。输入500而不是300 - 邮件不会隐藏,但它应该。 – 2009-04-29 07:35:07

+0

您是否重复了我之前评论的测试?仍然不起作用。 – 2009-04-29 09:02:06

+0

我重复了这个测试。这似乎是验证插件的一个错误。验证消息不会消失。然而,表单提交只发生在有效的输入(这是一件好事)。 – 2009-04-29 11:52:41