jquery validation custom rule input greather than

jquery validation custom rule input greather than

Hi Everyone

I would like to check if my value in my input is greater than or equal my stock.I check it with my custom rule jquery validator. I had a problem with my code when I enter a value in all my inputs they check, if I decide to change my first input, he no longer wants to check it. He only checks my last input but not the others.

This my jsfiddle

I think my problems come from my loop, but I'm not sure!

Thank you so much for your help.

  1. <form id="form_Ajout" name="formulaire"> <table id="TableSelect"> <tbody> <tr> <th>name</th> <th>price</th> <th>qte</th> </tr> <tr> <td class="tdQuantite"> Dell Latitude 7390</td> <td><input type="text" size="5" data="qte" name="QUANTITE_300" value="" class="stock"> </td> <td class="tdQuantite">74</td> </tr> <tr> <td class="tdQuantite"> Dell Latitude 7490</td> <td><input type="text" size="5" data="qte" name="QUANTITE_301" value="" class="stock"> </td> <td class="tdQuantite">10</td> </tr> <tr> <td class="tdQuantite"> Dell Latitude 5590</td> <td><input type="text" size="5" data="qte" name="QUANTITE_302" value="" class="stock"> </td> <td class="tdQuantite">100</td> </tr> </tbody> </table> <button type="submit" class="boutonValider" id="valid_form">Valider</button> </form>

This is my jquery code



  1. $(document).ready(function() { $(document).on('click', '#valid_form', function(e) { e.preventDefault(); var Stock = $('#TableSelect tr:not(:first-child) td input').filter(function() { return $(this).val() != ''; }).map(function() { let parent = jQuery(this).closest("tr"); return { quantite: parent.find("input").val(), stockReel: parent.find("td:nth-child(3)").text() } }); var stockR, Quantite; for(var i = 0 ; i < Stock.length; i++){ var stockR = Stock[i].stockReel; var Quantite = Stock[i].quantite; if (parseInt(Quantite) >= parseInt(stockR)){ console.log(' la quantité est ' +Quantite+ ' pour un restant de '+stockR); } } jQuery.validator.addMethod("CheckStockReel", function(value, element) { return this.optional(element) || (parseInt(value) <= parseInt(stockR)); }, "&nbsp; La quantité saisi est supérieure au stock réel."); $('.stock').each(function() { $(this).rules("add", { CheckStockReel: true, messages: { required: "&nbsp; &nbsp; La quantité saisi est supérieur au stock réel.", } }); }); }); }); $("#form_Ajout").validate();