Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

onchange client script not working

chandan86patra
Tera Contributor

Hi All,

I have written an onChange Client Script on the Account Number field to allow a maximum of 9 digits.

The script works as expected the first time. However, when I enter more than 9 digits for the second time, the field value is not cleared.

Could you please help me identify what is causing this issue?

 

 

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

var regexp = /^\d{1,9}$/;

if (!regexp.test(newValue)) {
g_form.showFieldMsg(
'account_number',
'Account number must contain a maximum of 9 digits.',
'error'
);

g_form.setValue('account_number', '');
}
}

1 REPLY 1

Brad Bowman
Mega Patron

Your script works fine for me in a mock-up in my PDI - every time I change the value of the field, not just the first time.  Do you possibly have any conflicting client scripts or UI policies also affecting this field?

 

To clarify the testing steps, I am:

1) entering 10 digits into the field - field clears

2) entering 10 digits into the field - field clears

3) entering 5 digits into the field - value remains

4) appending the field value to include 10 digits - field clears