- 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 10:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:37 AM
Did it finally work.. I tried the same in the test instance and see the same problem..
Created two column with the type of Integer.. On submit in the client script I gave in the below
var w1=g_form.getValue('weight1');
alert(w1);
var w2=g_form.getValue('weight2');
alert(w2);
var total = parseInt(w1) + parseInt(w2);
alert("The added sum is "+total);
or
the parseInt suggested by Chuck..
I get the Nan. Even though I have a value filled in the weight1 and Weight2 field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:41 AM
Yes It DID ...sCRIPT bELOW :-
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);
//alert(w1);
if(isNaN(w1))
w1 = 0;
var w2 = parseInt(g_form.getValue('weight_1'), 10);
//alert(w2);
if(isNaN(w2))
w2 = 0;
var w3 = parseInt(g_form.getValue('weight_3'), 10);
if(isNaN(w3))
w3 = 0;
var w4 = parseInt(g_form.getValue('weight_4'), 10);
if(isNaN(w4))
w4 = 0;
var w5 = parseInt(g_form.getValue('weight_5'), 10);
if(isNaN(w5))
w5 = 0;
var w6 = parseInt(g_form.getValue('weight_6'), 10);
if(isNaN(w6))
w6 = 0;
var w7 = parseInt(g_form.getValue('weight_7'), 10);
if(isNaN(w7))
w7 = 0;
var w8 = parseInt(g_form.getValue('weight_8'), 10);
if(isNaN(w8))
w8 = 0;
//alert(w8);
var w9 = parseInt(g_form.getValue('weight_9'), 10);
if(isNaN(w9))
w9 = 0;
var w10 = parseInt(g_form.getValue('weight_10'), 10);
if(isNaN(w10))
w10 = 0;
//var w = parseInt(w1)+parseInt(w2)+parseInt(w3)+parseInt(w4)+parseInt(w5)+parseInt(w6)+parseInt(w7)+parseInt(w8)+parseInt(w9)+parseInt(w10);
//var w = parseInt(w1+w2+w3+w4+w5+w6+w7+w8+w9+w10);
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;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:58 AM
Yeah it works. The script was not saved correctly..