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

client script is not working properly: onchange

chandan86patra
Tera Contributor

 

Hi All,

I have written an onChange Client Script for the Account Number field. The script is working as expected in both the ITIL view and Workspace.

Currently, I am using setTimeout() to clear the field value and display the validation message. However, since using setTimeout() is not considered a best practice in this scenario, could you please suggest an alternative approach to achieve the same functionality without using setTimeout()?

 

Hi All,

I have written an onChange Client Script for the Account Number field. The script is working as expected in both the ITIL view and Workspace.

Currently, I am using setTimeout() to clear the field value and display the validation message. However, since using setTimeout() is not considered a best practice in this scenario, could you please suggest an alternative approach to achieve the same functionality without using setTimeout()?

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var fieldName = 'u_account_number';

if (isLoading || newValue == '') {
return;
}

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

if (!regexp.test(newValue)) {
setTimeout(function() {
g_form.showFieldMsg(
fieldName,
'Account number must contain a maximum of 9 digits.',
'error'
);
g_form.setValue(fieldName, '');
}, 100);
}
}

 

0 REPLIES 0