On Change client script not working on integer like I'd expect.

phaug
Tera Contributor

I'm working on the OnChange client script below, expecting the error message to appear if a user tries to increase the value above 100 from the current value. I'd suspect since it's an integer my If statement would work, but it's not capturing the data as I'd expect. Any help is appreciated.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var newVal = parseInt(newValue);
   
    if (newVal > oldValue + 100){
        g_form.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        g_form.setValue(oldValue);
    }
       
}
9 REPLIES 9

Anand Kumar P
Giga Patron
Giga Patron

Hi @phaug ,

Use parseInt for old values as well

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var newVal = parseInt(newValue);
    if (newVal > (parseInt(oldValue) + 100)) {
        g_form.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        g_form.setValue(oldValue.toString());
    }
}

Please mark it as solution proposed and helpful if its serves your purpose.

Thanks,

Anand

phaug
Tera Contributor

I tried that change and it's still not working. The error message does not appear on the screen and I can still change the value.

Hi @phaug ,

I have tried in my PDI its working fine for me

AnandKumarP_0-1699291515132.png

 

AnandKumarP_1-1699291591811.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 
    var newVal = parseInt(newValue);
    if (newVal > (parseInt(oldValue) + 100)) {
        g_form.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        g_form.setValue('u_integer',oldValue.toString());
    }
   
   
}

Make sure onchange on integer field and ui type-all put info messages to testing.

Thanks,

Anand

I'm doing the same thing and it's still not working. I noticed if it's less than 1,000 it will show the error, but it does it for every single change. Doesn't matter if it's less than 100 or not. It just always throws the error, but never resets the number.