Onchange client script is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
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', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
56m ago
Hi there @chandan86patra
The could be due to the onChange script triggering again when g_form.setValue() clears the field.
You can simplify the validation like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || !newValue)
return;
if (!/^\d{1,9}$/.test(newValue)) {
g_form.showFieldMsg(
'account_number',
'Account number must contain a maximum of 9 digits.',
'error'
);
g_form.setValue('account_number', '');
}
}
It should work if it doesnt try using an onSubmit Client Script to validate the value before saving, or a UI Policy/.
Kind Regards,
Azar
Serivenow Rising Star ⭐
Architect@ KPMG.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
51m ago
Hello @chandan86patra ,
can you try these....
1 . clear the message upfront
2. use ClearValue instead setValue
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
// Clear any existing messages
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'
);
// Use clearValue instead of setValue to cleanly reset the field
g_form.clearValue('account_number');
}
}
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025,2026
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
25m ago
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || isTemplate) {
return;
}
var accountNumber = (newValue || '').trim();
// Allow user to clear the field manually
if (accountNumber == '') {
g_form.hideFieldMsg('account_number', true);
return;
}
var regexp = /^\d{1,9}$/;
if (!regexp.test(accountNumber)) {
// Clear first, then display the message
g_form.clearValue('account_number');
setTimeout(function () {
g_form.showFieldMsg(
'account_number',
'Account number must contain a maximum of 9 digits.',
'error'
);
}, 100);
} else {
g_form.hideFieldMsg('account_number', true);
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
