The required jQuery validation plugin module (dependency expression) is used normally, but the callback does not work

I am trying to do this job; this validate applies to the payment form, and, as you can see, there are only 3 inputs, if and only if

input[type=radio]:checked').val() != "CB"; 

this means that the user is about to pay something else but a credit card. Below is the complete code to validate my form.

 $("#paiement").validate({ errorClass: "invalid", validClass: "success", rules: { referenceTypeComplementBancaire: true, banque: { required: function(nomBanque){ return $('#paiement input[type=radio]:checked').val() != "CB"; } }, numeroComplementBancaire: { required: function(numeroCompl){ return $('#paiement input[type=radio]:checked').val() != "CB"; } }, montantComplementBancaire: { required: function(montantCompl){ var logg = montantRentre >= panierTotal; console.log(logg); return montantRentre >= panierTotal; } } }, messages: { referenceTypeComplementBancaire: "", banque:"", numeroComplementBancaire:"", montantComplementBancaire:"" } }); } 

Nothing complicated, really. But, it’s hard for me to understand why montantComplementBancaire is not checked, even if my console logging shows me β€œtrue” or β€œfalse” at the right time. What am I doing wrong?

---------------------------- EDIT ------------------ --- ---------------------

I think there was some misunderstanding, my fault. Sorry people. Here's what the form looks like:

  $("#paiement").validate({ errorClass: "invalid", validClass: "success", rules: { referenceTypeComplementBancaire: true, banque: { required: function(nomBanque){ return $('#paiement input[type=radio]:checked').val() != "CB"; } }, numeroComplementBancaire: { required: function(numeroCompl){ return $('#paiement input[type=radio]:checked').val() != "CB"; } }, montantComplementBancaire: { required: function(){ var logg = panierTotal > montantRentre; console.log(logg); return panierTotal > montantRentre; } } }, messages: { referenceTypeComplementBancaire: "", banque:"", numeroComplementBancaire:"", montantComplementBancaire:"" } }); 

Where am I wrong? Not only do I want the "montantComplementBancaire" to be checked, but I want it to be a valid IF and ONLY IF it> = to panierTotal

Right now, I'm testing only or not testing it, but what I'm looking for is a way to return "valid" when montantRentre> = to panierTotal.

Am I making it clearer?

+4
source share
3 answers

try return logg; or return (montantRentre >= panierTotal);

Your code looks fine and seems to follow the example here ( http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback ). The required property does not have to be an object with the dependent property, it can be a dependency-callback function, as you did.

You can also try

 if(montantRentre >= panierTotal) { return true; } else { return false; } 
+2
source

I do not think your verification is correct. The "required" property of each form element must be a JavaScript object with the "dependent" attribute set to the function, assuming that you are trying to do this conditional requirement. See the Rule entry on the validation options page: http://docs.jquery.com/Plugins/Validation/validate#toptions

I think the fixed code is:

 $("#paiement").validate({ errorClass: "invalid", validClass: "success", rules: { referenceTypeComplementBancaire: 'required', banque: { required: { depends: function(nomBanque){ return $('#paiement input[type=radio]:checked').val() != "CB"; } } }, numeroComplementBancaire: { required: { depends: function(numeroCompl){ return $('#paiement input[type=radio]:checked').val() != "CB"; } } }, montantComplementBancaire: { required: { depends: function(montantCompl){ var logg = montantRentre >= panierTotal; console.log(logg); return montantRentre >= panierTotal; } } } }, messages: { referenceTypeComplementBancaire: "", banque:"", numeroComplementBancaire:"", montantComplementBancaire:"" } }); } 
+2
source
 required: function(montantCompl){ var logg = montantRentre >= panierTotal; console.log(logg); return montantRentre >= panierTotal; } 

why do you define an argument to function(montantCompl){ that you never use? Did you mean to actually make your comparison with this? Even if you have not done so, it is actually not recommended to use global variables for comparison inside anonymous functions, as this leads to ambiguity, as now, when I try to help you solve your problem. If these values ​​are defined somewhere, please include them in your question, including any points at which they change state.

+1
source

All Articles