- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:24 AM
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!!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:13 AM
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:26 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:30 AM
But they are already integer fields Chuck! Still do I have to do that?
I am getting right alert for w1 / w2
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:34 AM
Yes. Either way you need to get strings converted to integers. getValue() is interesting when it comes to non-string values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:43 AM
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;
}
}
}