2009-04-29 21 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