Bringing up an alert with onChange client script

davilu
Mega Sage

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!

1 ACCEPTED SOLUTION

Bhojraj Dhakate
Tera Expert

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:

find_real_file.png

Thanks,

Bhojraj

View solution in original post

8 REPLIES 8

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...

Can you use String Field ?

if you use string fields above logic works.

Thanks,

Bhojraj

yea, i switched it to String and it works fine.  just curious why Integers doesn't work...

 

thanks!

Wirasat
Tera Guru

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.');
}

}