- 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:54 AM
FYI, you don't need to do parseInt() twice per variable. Either do it when you read getValue() or when you do the addition.
Also, When I use it, always make sure to use "10" as the second argument as in
var w3 = parseInt(g_form.getValue('weight_3'), 10);
to ensure decimal precision so there is no ambiguity.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:04 AM
Hi ctomasi,
The script is not working in any way I modify it!
Please can you paste the script that'll work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:09 AM
Let's start small just to ensure things are working as expected, then extend out once we've verified it. Try this and tell me what you see.
function onSubmit() {
var w1=g_form.getValue('weight');
var w2=g_form.getValue('weight_1');
var i1 = parseInt(w1, 10);
var i2 = parseInt(w2, 10);
var w = (i1+i2);
alert(w);
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:38 AM
I guess "/"(Divide) operator is only for numbers. So, javascript converted them as numbers and performed the division operation. But + operator is used both for addition and concatenation. By default getValue() returned string. So, it concatenating them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 09:27 AM
Hi Arnab,
getValue() method returns string, so convert it to integer using parseInt method. If decimals are also allowed, use parseFloat() method.
var w1= parseInt(g_form.getValue('weight'));