Question

J'utilise le plug-in de validation de jQuery pour vérifier les entrées positives de l'utilisateur cost_of_car dans le premier champ texte et positif payment_amount dans un deuxième champ texte tel que * 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 
        }
    }
});

Je souhaite maintenant ignorer les contrôles testMaxAmount et testMinAmount jusqu'à ce que cost_of_car soit valide. Test

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

ou même

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

Dans ces méthodes, la récursivité et le navigateur sont bloqués.

Proposeriez-vous une autre méthode pour désactiver la validation de payment_amount jusqu'à ce que cost_of_car soit valide, s'il vous plaît?

Était-ce utile?

La solution

UPDATE: Le changement doit être effectué dans les validator.addMethod() appels:

.
$.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");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top