Help required with addition in a Client Script

arnabbose
Tera Expert

Hi Experts,

I am trying to do a wright% validation on my form with below Client Script. If total weight is more or less than 100% ; alert should throw.

function onSubmit() {

  //Type appropriate comment here, and begin script below

  process_req_type();

  function process_req_type(){

  var w1=g_form.getValue('weight');

  alert(w1);

  var w2=g_form.getValue('weight_1');

  alert(w2);

  var w3=g_form.getValue('weight_3');

  var w4=g_form.getValue('weight_4');

  var w5=g_form.getValue('weight_5');

  var w6=g_form.getValue('weight_6');

  var w7=g_form.getValue('weight_7');

  var w8=g_form.getValue('weight_8');

  var w9=g_form.getValue('weight_9');

  var w10=g_form.getValue('weight_10');

  var w = (w1+w2+w3+w4+w5+w6+w7+w8+w9+w10);

  alert(w);

  if ( w == 100)

  {

  g_form.setValue ('total_weight', Math.round(w));

  }

  else {

  alert ("Total weight% cannot be less or more than 100% , please re-enter values");

  g_form.setValue ('total_weight', "");

  g_form.setValue ('weight', "");

  g_form.setValue ('weight_1', "");

  g_form.setValue ('weight_3', "");

  g_form.setValue ('weight_4', "");

  g_form.setValue ('weight_5', "");

  g_form.setValue ('weight_6', "");

  g_form.setValue ('weight_7', "");

  g_form.setValue ('weight_8', "");

  g_form.setValue ('weight_9', "");

  g_form.setValue ('weight_10', "");

  return false;

  }

  }

}

The issue is that the addition is not happening and w is stroing concatenated values from variables w1 , w2, w3 etc.

For example, I put 50 in w 1 and w2 -- instead of 100 , w holds 5050.

Please help me resolve this!!!

find_real_file.png

1 ACCEPTED SOLUTION

Often these are the mistakes developers usually do. If we convert



If we try to convert space into integer, we will get NaN. If any of those weight fields are blank, you will get NaN value. So, you need to do some empty check or NaN check. Once you parsed to integers using parseInt function,   do NaN checks and if they are NaN, make them 0.




var w1 =     parseInt(g_form.getValue('weight'), 10);


if(isNaN(w1))


      w1 = 0;


var w2 =     parseInt(g_form.getValue('weight_1'), 10);


if(isNaN(w2))


      w2 = 0;


View solution in original post

23 REPLIES 23

Chuck Tomasi
Tera Patron

You need to convert them to integers using parseInt() - a standard JavaScript method.



var w3 = parseInt(g_form.getValue('weight_3'), 10);



// For all variables.


ctomasi



But they are already integer fields Chuck! Still do I have to do that?



I am getting right alert for w1 / w2



find_real_file.png


Also, the issue is not with variables , the issue is with the addition and storing in w



var w = (w1+w2+w3+w4+w5+w6+w7+w8+w9+w10);



Please advise!


Yes. Either way you need to get strings converted to integers. getValue() is interesting when it comes to non-string values.


ctomasi



Below is my script now; I am getting a NaN alert and its not working!



function onSubmit() {


  //Type appropriate comment here, and begin script below


  process_req_type();



  function process_req_type(){



  var w1 = parseInt(g_form.getValue('weight'), 10);


  var w2 = parseInt(g_form.getValue('weight_1'), 10);


  var w3 = parseInt(g_form.getValue('weight_3'), 10);


  var w4 = parseInt(g_form.getValue('weight_4'), 10);


  var w5 = parseInt(g_form.getValue('weight_5'), 10);


  var w6 = parseInt(g_form.getValue('weight_6'), 10);


  var w7 = parseInt(g_form.getValue('weight_7'), 10);


  var w8 = parseInt(g_form.getValue('weight_8'), 10);


  var w9 = parseInt(g_form.getValue('weight_9'), 10);


  var w10 = parseInt(g_form.getValue('weight_10'), 10);



  var w = parseInt(w1)+parseInt(w2)+parseInt(w3)+parseInt(w4)+parseInt(w5)+parseInt(w6)+parseInt(w7)+parseInt(w8)+parseInt(w9)+parseInt(w10);


  alert(w);



  if ( w == 100)



  {


  g_form.setValue ('total_weight', Math.round(w));


  }


  else {


  alert ("Total weight% cannot be less or more than 100% , please re-enter values");


  g_form.setValue ('total_weight', "");


  g_form.setValue ('weight', "");


  g_form.setValue ('weight_1', "");


  g_form.setValue ('weight_3', "");


  g_form.setValue ('weight_4', "");


  g_form.setValue ('weight_5', "");


  g_form.setValue ('weight_6', "");


  g_form.setValue ('weight_7', "");


  g_form.setValue ('weight_8', "");


  g_form.setValue ('weight_9', "");


  g_form.setValue ('weight_10', "");



  return false;


  }


  }


}