كيفية تعطيل التحقق من صحة مخصصة الأسلوب على الشرط

StackOverflow https://stackoverflow.com/questions/800983

  •  03-07-2019
  •  | 
  •  

سؤال

يمكنني استخدام مسج التحقق من صحة البرنامج المساعد لضمان ما دخل المستخدم إيجابية cost_of_car في أول حقل النص و إيجابية 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 
        }
    }
});

الآن أريد أن تخطي testMaxAmount و testMinAmount يتحقق حتى cost_of_car هو صالح.اختبار

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

أو حتى

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

داخل هذه الطرق يؤدي إلى العودية و توقف المتصفح.

تقترح طريقة أخرى لتعطيل التحقق من صحة payment_amount حتى cost_of_car صالحة, من فضلك ؟

هل كانت مفيدة؟

المحلول

تحديث: التغيير يجب أن يكون في 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");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top