1

jQuery Validation プラグインを使用して、どのユーザーが最初のテキスト フィールドに正のcost_of_carを入力し、2 番目のテキスト フィールドに正のpayment_amountを入力したかを確認します。*cost_of_car * 0.4 <= payment_amount <= 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 
        }
    }
});

cost_of_carが有効になるまで、testMaxAmount と testMinAmount のチェックをスキップしたいと思います。テスト

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

あるいは

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

これらのメソッド内で再帰が発生し、ブラウザーがハングします。

cost_of_carが有効になるまでpayment_amountの検証を無効にする別の方法を提案していただけませんか?

4

1 に答える 1

2

更新:変更は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");
于 2009-04-29T05:36:00.663 に答える