Onchange client script is not working

chandan86patra
Tera Contributor

Hi All , 

 

I have written the on change client script  on the account field , on the first time this  script is working fine but in the second time changing the  field value ( giving more than 9 digits ) it is not clearing the value , could you please help me on this script :

 

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

9 REPLIES 9

@Ankur Bawiskar @Tanushree Maiti it is working fine in ITIL view or normal view but it is not working in workpsce 

Hi @chandan86patra 

 

Ensure UI Type of your client script has been set to ALL

Refer: KB0827632 How to configure a client script to run in both platform UI and Workspace 

 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

@chandan86patra 

things to check

-> is UI type - ALL

-> did you mention particular view in the view field on client script

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Tanushree Maiti
Tera Patron

Hi @chandan86patra 

 

Try this- 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('account_number' ,true);
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.clearValue('account_number'); 
}

}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Vishal Jaswal
Tera Sage

Hello @chandan86patra 

Use setValue before showFieldMsg as your code is correct:

VishalJaswal_0-1791904691986.png



Validation Results

SOW:

VishalJaswal_1-1791904714822.png


Native UI:

VishalJaswal_2-1791904743160.png

 




Hope that helps!