- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 12:09 PM
I have two integer fields on a form and want an alert to show up if the sum of those two fields exceeds 6000. I am trying to do this with an onChange client script that looks like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var a = parseInt(g_form.getValue('field1'));
var b = parseInt(g_form.getValue('field2'));
if(a+b > 6000) {
alert('Total cannot exceed 6,000.');
}
}
What do I need to change in the above code in order for this to work? Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 12:38 PM
Hi davilu,
Please find below OnSubmit Client Script. It Working for me.
Code:
function onSubmit() {
//Type appropriate comment here, and begin script below
var strt = parseInt(g_form.getValue('a'));
var end =parseInt( g_form.getValue('b'));
var C = strt + end;
g_form.setValue('c',C);
if(C > 6000) {
alert('Total cannot exceed 6,000.');
return false;
}
else
return true;
}
Screenshot:
Thanks,
Bhojraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 01:22 PM
hey raj, my fields were initially Integer fields. I made a = 2,000 and b = 5,000 and when I did alert(c), just to see if the sum would return to be 7,000, the alert showed only 7. When I removed parseInt, the alert came back as 2,0005,000 (just a simple concatenation). This makes zero sense to me, but I managed to fix it by changing my fields to Strings instead of Integers.
Do you know why an Integer field would return something so strange? It almost seems like the code you provided doesn't take into account anything after the comma for each value for a and b...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 01:39 PM
Can you use String Field ?
if you use string fields above logic works.
Thanks,
Bhojraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 02:41 PM
yea, i switched it to String and it works fine. just curious why Integers doesn't work...
thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 02:32 PM
Your script looks fine. I just ran the same script with log information and it ran fine. Only thing that I can think of is typo in field1 and field 2.
Here is the script that I ran it executed fine
function onSubmit() {
//Type appropriate comment here, and begin script below
var a = parseInt(g_form.getValue('field1'));
var b = parseInt(g_form.getValue('field2'));
console.log("a=" + a);
console.log("b=" + b);
jslog("a=" + a);
jslog("b=" + b);
if(a+b > 6000) {
alert('Total cannot exceed 6,000.');
}
}