Calculate input values and check if it is === to database field value.

Hello guys,

Again, I need your talent !

I need to calculate the total value (number) of 4 inputs and make a correspondance with a number in my database.

If the total value of the 4 inputs match the number in the database, it show/allows de validation button.
If not, the validation button must by hided/disable.

Here is something I tried (don’t laugh at me :P, beginner in auto-practice !) :

   $w.onReady( () => { 
$w("#datasetMB").onReady( () => {
 let NbreP1 = $w('#input1').value;
 let NbreP2 = $w('#input2').value;
 let NbreP3 = $w('#input3').value;
 let NbreP4 = $w('#input4').value;
 let NbreTotal = $w("#datasetMB").getCurrentItem().totalNbreLivres;
 		if(NbreP1 + NbreP2 + NbreP3 + NbreP4 === NbreTotal) {
	$w("#buttonValider").show();
 	 }
 	        else {
	$w("#buttonValider").show();
  }
} );
  } );

But, as you know now, it is not working ;).

Thanks for your help guys !

So according to your code, if something is correct then your #buttonValider is to be shown, however yet if something is wrong your #buttonValider is still set to be shown…

$w("#buttonValider").show();
}
else {
$w("#buttonValider").show();

Yes, my mistake. But even this the correct hide(), doesn’t work…
Also, I guess I need somewhere an onChange event : when input value is changing, button hide or show accordingly. But no idea how to organize the code.

Here is what I need to do :

Hello @yisrael-wix , I am blocked for an entire week with that, could you help a quite desperate woman like me please ? :stuck_out_tongue:

You have to add event listeners to the input that will detect value change. Otherwise the values will be undefined.
Inside the dataset.onReady, do something like this:

$w("#input1, #input2, #input3, #input4").onChange(event => {
    let allValues = [$w("#input1").value, $w("#input2").value, $w("#input3").value, $w("#input4").value];
    allValues = allValues.filter(e => e);//filter out undefined values
    let sum = allValues.reduce((a,b) => a +  b, 0);
    if ( sum === NbreTotal){
        //do whatever you want
    } else {
        //do whatever you want;
    }
})